Skip to content

Instantly share code, notes, and snippets.

@martinthenext
Created July 31, 2018 19:40
Show Gist options
  • Save martinthenext/d2e7cfdba6fe43a2abac5d404b871e0a to your computer and use it in GitHub Desktop.
Save martinthenext/d2e7cfdba6fe43a2abac5d404b871e0a to your computer and use it in GitHub Desktop.
Read CSV to dicts
def read_csv_to_dicts(filename):
with open(filename) as csvfile:
reader = csv.reader(csvfile, dialect='excel')
header = transform_header(next(reader))
if len(header) != len(set(header)):
raise ValueError("Duplicate column names in CSV.")
data = [{field: value for field, value in zip(header, row)}
for row in reader]
if 'doc_id' not in header:
data = [{'doc_id': 'doc{}'.format(i), **d}
for i, d in enumerate(data)]
return data
@tbmreza
Copy link

tbmreza commented Jun 13, 2019

what does transform_header do?

@martinthenext
Copy link
Author

martinthenext commented Jun 18, 2019

@tbmreza I don't remember anymore, sorry!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment