Skip to content

Instantly share code, notes, and snippets.

@joshgeller joshgeller/importcsv.py
Last active Aug 29, 2015

Embed
What would you like to do?
Add contents of specified columns in .csv file to a list of dictionaries
import csv
def import_csv(filename=None, import_all_columns=False, columns_to_import=None, remove_duplicates=False):
"""
Add contents of specified columns in .csv file to a list of dictionaries.
:param filename: string
:param import_all_columns: bool
:param columns_to_import: list
:param remove_duplicates: bool
:return: list of dictionaries
"""
# Avoid mutable function defaults
if columns_to_import is None:
columns_to_import = []
results = []
with open(filename, 'rb') as c:
csv_reader = csv.DictReader(c)
for row in csv_reader:
if import_all_columns:
results.append(row)
else:
results.append({key: value for (key, value) in row.items() if key in columns_to_import})
if remove_duplicates:
return [dict(t) for t in set([tuple(d.items()) for d in results])]
return results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.