Created
November 10, 2018 20:40
-
-
Save EricCousineau-TRI/ecec49b8c92a73d3b65c8bc13a9c40da 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
| #!/usr/bin/python | |
| import argparse | |
| import csv | |
| import sys | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("input", type=argparse.FileType("r")) | |
| parser.add_argument("-o", "--output", type=argparse.FileType("w"), default=None) | |
| parser.add_argument("-i", "--in-place", action="store_true") | |
| args = parser.parse_args() | |
| fin = args.input | |
| fout = args.output | |
| with fin: | |
| r = csv.reader(fin) | |
| fieldnames = r.next() | |
| rows = [row for row in r] | |
| rows = sorted(rows) | |
| if args.in_place: | |
| assert fout is None | |
| assert fin != sys.stdin | |
| fout = open(fin.name, 'w') | |
| else: | |
| fout = sys.stdout | |
| with fout: | |
| w = csv.writer(fout, quoting=csv.QUOTE_ALL) | |
| w.writerow(fieldnames) | |
| for row in rows: | |
| w.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment