jpayne@0: #!/usr/bin/env python3 jpayne@0: jpayne@0: import csv jpayne@0: import sys jpayne@0: jpayne@0: def main(headers): jpayne@0: rows = csv.DictReader(sys.stdin, delimiter='\t', dialect='excel-tab') jpayne@0: if not any([str(header) in rows.fieldnames for header in headers]): jpayne@0: raise ValueError("Couldn't find any of supplied headers ({}) in the table.".format(','.join(['"{}"'.format(header) for header in headers]))) jpayne@0: items = list(rows) jpayne@0: items.sort(key=lambda d: [d.get(h) or "" for h in headers]) jpayne@0: wr = csv.DictWriter(sys.stdout, dialect='excel-tab', fieldnames=rows.fieldnames) jpayne@0: wr.writeheader() jpayne@0: wr.writerows(items) jpayne@0: sys.stdout.flush() jpayne@0: jpayne@0: if __name__ == '__main__': jpayne@0: main(sys.argv[1:])