Created
December 20, 2013 03:48
-
-
Save stevekrouse/8050139 to your computer and use it in GitHub Desktop.
csvs to and from dics
This file contains hidden or 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 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