Skip to content

Instantly share code, notes, and snippets.

@dogrdon
Created March 7, 2017 17:45
Show Gist options
  • Save dogrdon/6835ef055c25cec6eb1b6e0c6193cfed to your computer and use it in GitHub Desktop.
Save dogrdon/6835ef055c25cec6eb1b6e0c6193cfed to your computer and use it in GitHub Desktop.
python script to scramble some csvs
'''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