Skip to content

Commit

Permalink
add parameters to read_csv_to_dict()
Browse files Browse the repository at this point in the history
  • Loading branch information
skasberger committed Jul 3, 2019
1 parent 761f39d commit edc48da
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/pyDataverse/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def read_file_csv(filename):
csvfile.close()


def read_csv_to_dict(filename):
def read_csv_to_dict(filename, delimiter=';', quotechar='"', encoding='utf-8'):
"""Read in csv file and convert it into a list of dicts.
This offers an easy import functionality of csv files with dataset metadata.
Expand All @@ -183,6 +183,12 @@ def read_csv_to_dict(filename):
----------
filename : string
Filename with full path.
delimiter : string
Cell delimiter of CSV file. Defaults to ';'.
quotechar : string
Quote-character of CSV file. Defaults to '"'.
encoding : string
Character encoding of file. Defaults to 'utf-8'.
Returns
-------
Expand All @@ -192,8 +198,9 @@ def read_csv_to_dict(filename):
dataset metadata naming convention.
"""
reader = csv.DictReader(open(filename), 'r')
data = []
for row in reader:
data.append(dict(row))
with open(filename, 'r', newline='', encoding=encoding) as csvfile:
reader = csv.DictReader(csvfile, delimiter=delimiter, quotechar=quotechar)
data = []
for row in reader:
data.append(dict(row))
return data

0 comments on commit edc48da

Please sign in to comment.