view table-sort.py @ 8:fa9bd5c8fae0

planemo upload
author jpayne
date Tue, 30 Jan 2018 15:54:55 -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:])