Skip to content

Instantly share code, notes, and snippets.

@jion
Last active April 2, 2022 23:58
Show Gist options
  • Save jion/3244ea72d14facb3d791ed213117f5a4 to your computer and use it in GitHub Desktop.
Save jion/3244ea72d14facb3d791ed213117f5a4 to your computer and use it in GitHub Desktop.
Helper to filter CSV columns you need
#!/usr/bin/python3
import csv
import sys
csv_reader = csv.DictReader(sys.stdin)
csv_writer = csv.DictWriter(sys.stdout, fieldnames=[h for h in csv_reader.fieldnames if h in sys.argv[1:]])
csv_writer.writeheader()
for row in csv_reader:
csv_writer.writerow({f: row[f] for f in sys.argv[1:]})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment