Last active
August 29, 2015 14:04
-
-
Save joshgeller/50166883763a738af6f4 to your computer and use it in GitHub Desktop.
Add contents of specified columns in .csv file to a list of dictionaries
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 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