Skip to content

Instantly share code, notes, and snippets.

@csmoore
Created July 21, 2014 14:59
Show Gist options
  • Save csmoore/f162e35e834afe1356fa to your computer and use it in GitHub Desktop.
Save csmoore/f162e35e834afe1356fa to your computer and use it in GitHub Desktop.
import csv
# Python Simple csv writer/reader snippets
# NOTE: "rb" "wb" is important csv needs these to be opened in "binary" format
###################################################
# csv writer snippet
f_out = open(outputFile, 'wb')
writer = csv.writer(f_out, delimiter=',')
writer.writerow(["COL1", "COL2", "COL3"]) # make header row
# write your outputs
outRow = [val1, val2, val3]
writer.writerow(outRow)
###################################################
# csv reader snippet
with open(inputFile, "rb") as f_in :
reader = csv.reader(f_in, delimiter=',')
next(reader, None) # skip header row (assumes there is one)
# if your data has a unique id/key, you can just throw in a dictionary for easy lookup later
# in this example, row[0] (column 0) has the unqiue key
csvDict = {row[0]:row for row in reader}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment