Created
March 7, 2017 17:45
-
-
Save dogrdon/6835ef055c25cec6eb1b6e0c6193cfed to your computer and use it in GitHub Desktop.
python script to scramble some csvs
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
'''SOMETIMES YOU WANT SOME SCRAMBLED CSVs: python scrambled_csv.py <path_to_csv_file>''' | |
import csv, random | |
import sys | |
def scrambler(head, cols): | |
scrambled = [] | |
for i,e in enumerate(head): | |
col = [x[i] for x in cols] | |
scrambled.append(col) | |
for s in scrambled: | |
random.shuffle(s) | |
return zip(*scrambled) | |
if __name__ == '__main__': | |
inputfile = sys.argv[1] | |
outputfile = "{0}_scrambled.csv".format(inputfile.replace('.csv', '')) | |
with open(inputfile, 'r') as f: | |
rs = csv.reader(f) | |
head, l = next(rs), list(rs) | |
scrambled = scrambler(head, l) | |
print outputfile | |
with open(outputfile, 'w') as o: | |
w = csv.writer(o) | |
w.writerow(head) | |
for s in scrambled: | |
w.writerow(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment