Mercurial > repos > jpayne > table_ops
view table-sort.py @ 12:15230438f7f5
planemo upload
author | jpayne |
---|---|
date | Mon, 12 Feb 2018 10:22:47 -0500 |
parents | f1f2497301d3 |
children |
line wrap: on
line source
#!/usr/bin/env python3 import csv import sys def main(headers): rows = csv.DictReader(sys.stdin, delimiter='\t', dialect='excel-tab') if not any([str(header) in rows.fieldnames for header in headers]): raise ValueError("Couldn't find any of supplied headers ({}) in the table.".format(','.join(['"{}"'.format(header) for header in headers]))) items = list(rows) items.sort(key=lambda d: [d.get(h) or "" for h in headers]) wr = csv.DictWriter(sys.stdout, dialect='excel-tab', fieldnames=rows.fieldnames) wr.writeheader() wr.writerows(items) sys.stdout.flush() if __name__ == '__main__': main(sys.argv[1:])