I have a csv file that looks like this (in reality it is bigger):
JavaScript
x
5
1
country;company1;company2;company3
2
finland;30;30;40
3
sweden;20;30;50
4
norway;10;20;70
5
How could I read this file the easiest, so that I could get a dictionary like this (dictionaries inside a dictionary):
JavaScript
1
5
1
{ 'company1': {'finland': 30, 'sweden': 20, 'norway': 10}
2
'company2': {'finland': 30, 'sweden': 30, 'norway': 20}
3
4
}
5
I first tried to make a separate list from the first of the file, aka from the companies, then created a dictionary from them. But then I ran into problems while trying to read the lines after the first one and create a dictionary inside the already created one.
I’m sorry if the explanation is bad, I’m new to coding!
Advertisement
Answer
@fsimonjetz’s answer is great if you are already working with pandas in this project. If you are not, using it just for this task is a huge overkill, as we can parse and transpose the data with a simple logic.
JavaScript
1
19
19
1
import csv
2
3
from collections import defaultdict
4
5
output = defaultdict(dict)
6
7
with open('path/to/your.csv') as f:
8
reader = csv.DictReader(f, delimiter=';')
9
companies = reader.fieldnames[1:]
10
for line in reader:
11
country = line['country']
12
for company in companies:
13
output[company][country] = line[company]
14
# or output.setdefault(company, {})[country] = line[company]
15
# if you want 'output' to be a "normal" dict instead of defaultdict
16
17
print(dict(output)) # or just print(output) if you don't mind seeing OrderedDict
18
# repr
19
outputs
JavaScript
1
4
1
{'company1': {'finland': '30', 'sweden': '20', 'norway': '10'},
2
'company2': {'finland': '30', 'sweden': '30', 'norway': '20'},
3
'company3': {'finland': '40', 'sweden': '50', 'norway': '70'}}
4