I want to read a csv as a dictionary in Python, but now I encountered a problem, because the csv contains headers used more than once, like this:
id | name | labels | labels |
---|---|---|---|
01 | one | mytask | myproduct |
02 | two | mylabel |
The standard way to import a csv to python looks like this:
# import csvimport csv# read csv file to a list of dictionarieswith open('data.csv', 'r') as file: csv_reader = csv.DictReader(file) data = [row for row in csv_reader]print(data)
Sadly this code swallows up the first 'labels' value if there is more than one. This code outputs this:
[ {'id': '01', 'name': 'one', 'labels': 'myproduct'}, {'id': '02', 'name': 'two', 'labels': 'mylabel'},]
Is there any way to read also the second value for 'labels' without getting to complicated? My preferred output would look like this:
[ {'id': '01', 'name': 'one', 'labels': ['mytask', 'myproduct']}, {'id': '02', 'name': 'two', 'labels': 'mylabel'},]