Skip to content

Instantly share code, notes, and snippets.

@fakechris
Created January 6, 2013 06:19
Show Gist options
  • Select an option

  • Save fakechris/4465601 to your computer and use it in GitHub Desktop.

Select an option

Save fakechris/4465601 to your computer and use it in GitHub Desktop.
import csv,sys
def fn_sort_by(key, header_index):
return lambda x,y:cmp(x[header_index[key]], y[header_index[key]])
if __name__ == "__main__":
csvfilename = sys.argv[1]
sort_fileds = sys.argv[2]
f = open(csvfilename)
header = f.readline().split(',')
header_index = dict(zip(header, range(len(header))))
csvfile = list(csv.reader(f))
for field in sort_fields.split(","):
csvfile.sort(fn_sort_by(field, header_index))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment