Mercurial > repos > jpayne > table_ops
comparison table-sort.py @ 0:f1f2497301d3
planemo upload
author | jpayne |
---|---|
date | Mon, 08 Jan 2018 11:19:54 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f1f2497301d3 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 import csv | |
4 import sys | |
5 | |
6 def main(headers): | |
7 rows = csv.DictReader(sys.stdin, delimiter='\t', dialect='excel-tab') | |
8 if not any([str(header) in rows.fieldnames for header in headers]): | |
9 raise ValueError("Couldn't find any of supplied headers ({}) in the table.".format(','.join(['"{}"'.format(header) for header in headers]))) | |
10 items = list(rows) | |
11 items.sort(key=lambda d: [d.get(h) or "" for h in headers]) | |
12 wr = csv.DictWriter(sys.stdout, dialect='excel-tab', fieldnames=rows.fieldnames) | |
13 wr.writeheader() | |
14 wr.writerows(items) | |
15 sys.stdout.flush() | |
16 | |
17 if __name__ == '__main__': | |
18 main(sys.argv[1:]) |