Created
July 31, 2018 19:40
-
-
Save martinthenext/d2e7cfdba6fe43a2abac5d404b871e0a to your computer and use it in GitHub Desktop.
Read CSV to dicts
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@tbmreza I don't remember anymore, sorry!