Last active
November 23, 2019 16:50
-
-
Save u8sand/84ea2e108a9483148b30 to your computer and use it in GitHub Desktop.
Downsample lines in a file, useful for csv's that are too big.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
''' | |
Usage: | |
python downsample.py [offset+]amount | |
Examples: | |
cat super_big.csv | python downsample.py 1+4 > big_divided_by_4.csv | |
cat data.csv | python downsample.py 2 > data_halved.csv | |
''' | |
import sys | |
from itertools import islice | |
try: | |
if len(sys.argv) != 2: | |
raise Exception("Accepts one and only one argument") | |
args = sys.argv[1].split('+') | |
if len(args) == 2: | |
offset, amount = map(int, args) | |
elif len(args) == 1: | |
offset, (amount,) = 0, map(int, args) | |
else: | |
raise Exception("Only one `+` should be specified") | |
for line in islice(sys.stdin, offset, None, amount): | |
sys.stdout.write(line) | |
except Exception as e: | |
print(e, file=sys.stderr) | |
print('Must specify downsample amount ([offset+]amount)', file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment