Last active
May 15, 2019 21:37
-
-
Save mrcoles/48beb21cfeec0262e56d292a4b691bbd to your computer and use it in GitHub Desktop.
Simple template for a python script that uses argparse and reads in and prints out a CSV or TSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
def run(infile, outfile, is_tsv=False, dry_run=False): | |
dialect = 'excel-tab' if is_tsv else 'excel' | |
reader = csv.DictReader(infile, dialect=dialect) | |
writer = None | |
is_first = True | |
for row in reader: | |
if is_first: | |
# only write to writer if there is any data in reader | |
is_first = False | |
fieldnames = reader.fieldnames | |
writer = csv.DictWriter(outfile, fieldnames=fieldnames) | |
writer.writeheader() | |
writer.writerow(row) | |
## | |
def main(): | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser(description='Parse CSV file') | |
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | |
default=sys.stdin, help='input file or stdin') | |
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), | |
default=sys.stdout, help='output file or stdout') | |
parser.add_argument('--tsv', '-t', action='store_true', default=False) | |
parser.add_argument('--dry-run', '-n', action='store_true', default=False) | |
args = parser.parse_args() | |
run(args.infile, args.outfile, args.tsv, args.dry_run) | |
if __name__ == '__main__': | |
main() | |
# NOTE: if _csv.Error field larger than field limit | |
# take a look at https://stackoverflow.com/a/15063941/376489 | |
# and maybe set `csv.field_size_limit(sys.maxsize)` | |
# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment