Skip to content

Instantly share code, notes, and snippets.

@alex-r-bigelow
Created February 24, 2017 17:13
Show Gist options
  • Save alex-r-bigelow/b482b00845d853316b49f241bed2eb1b to your computer and use it in GitHub Desktop.
Save alex-r-bigelow/b482b00845d853316b49f241bed2eb1b to your computer and use it in GitHub Desktop.
Basic boilerplate for parsing a csv file
#!/usr/bin/env python
import csv
import argparse
parser = argparse.ArgumentParser(description='Boilerplate for ' +
'processing a CSV file')
parser.add_argument('-i', '--input', dest='input', default='input.csv',
help='The input file (default=input.csv)')
parser.add_argument('-o', '--output', dest='output', default='output.csv',
help='The output file')
args = parser.parse_args()
infile = open(args.input, 'rU')
outfile = open(args.output, 'wb')
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, reader.fieldnames)
writer.writeheader()
for rowNumber, line in enumerate(reader):
# Your reshaping here!
writer.writerow(line)
if rowNumber % 100 == 0:
print '.',
if rowNumber % 1000 == 0:
print 'processed %i rows' % rowNumber
infile.close()
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment