-
-
Save cadrev/2ad73d5d1b84047faaac2db6dec938d0 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 hidden or 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 cycle | |
if len(sys.argv)==1: | |
print('Must specify downsample amount ([offset+]amount)', file=sys.stderr) | |
else: | |
arg = sys.argv[1] | |
if arg.find('+')!=-1: | |
delay, amnt = map(int, arg.split('+')) | |
for a in range(delay): | |
sys.stdout.write(next(sys.stdin)) | |
else: | |
amnt = int(arg) | |
n = cycle(range(amnt)) | |
for l in sys.stdin: | |
if next(n) == 0: | |
sys.stdout.write(l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment