Created
February 24, 2017 17:13
-
-
Save alex-r-bigelow/b482b00845d853316b49f241bed2eb1b to your computer and use it in GitHub Desktop.
Basic boilerplate for parsing a csv file
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
#!/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