Skip to content

Instantly share code, notes, and snippets.

@stevekrouse
Created December 20, 2013 03:48
Show Gist options
  • Save stevekrouse/8050139 to your computer and use it in GitHub Desktop.
Save stevekrouse/8050139 to your computer and use it in GitHub Desktop.
csvs to and from dics
import csv
def dicts_from_csv(filename):
with open(filename, 'rU') as f:
reader = csv.DictReader(f)
return [row for row in reader]
def csv_from_dicts(filename, dicts, fieldnames=None):
if not fieldnames:
fieldnames = list(set([key for d in dicts for key in d.iterkeys()]))
with open(filename, 'w+') as f:
writer = csv.DictWriter(f, fieldnames)
writer.writeheader()
writer.writerows(dicts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment