I implemented the follwoing csv to dict method:
def csv_to_dict(path):
res = {}
with open(path, newline='') as csv_file:
reader = csv.DictReader(csv_file)
for row in reader:
for key, value in row.items():
if key in res:
res[key].append(value)
else:
res[key] = [value]
return res
This methods works as desired, except an issue
If I take the following input .csv
Lfd. Nr.,Parameter 1,Parameter 2
1,2,3
1,2,3
The output of the above function is on MAc OS:
{'\ufeffLfd. Nr.': ['1', '1'], 'Parameter 1': ['2','2'], 'Parameter 2' : ...
On Windows I get the following output:
{"u00efu00bbu00bfLfd. Nr.": ['1', '1'], 'Parameter 1': ['2','2'], 'Parameter 2' : ...
How to get rid off these characters in the first key? Why are these there?
Desired output:
{'Lfd. Nr.': ['1', '1'], 'Parameter 1': ['2','2'], 'Parameter 2' : ...
UPDATE my csv file is encoded in utf-8.
I also tried (without success):
with open(path, newline='', encoding='utf-8') as csv_file:
Solution based on marked link to SO question:
with open(path,"r", newline='',encoding='utf-8-sig') as csv_file