diff table-sort.py @ 0:f1f2497301d3

planemo upload
author jpayne
date Mon, 08 Jan 2018 11:19:54 -0500
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/table-sort.py	Mon Jan 08 11:19:54 2018 -0500
@@ -0,0 +1,18 @@
+#!/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:])
\ No newline at end of file