Created
January 6, 2013 06:19
-
-
Save fakechris/4465601 to your computer and use it in GitHub Desktop.
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
| 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