annotate table-sort.py @ 4:4a5c79572664

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