I have a csv file that looks like this (in reality it is bigger):
country;company1;company2;company3 finland;30;30;40 sweden;20;30;50 norway;10;20;70
How could I read this file the easiest, so that I could get a dictionary like this (dictionaries inside a dictionary):
{ 'company1': {'finland': 30, 'sweden': 20, 'norway': 10} 'company2': {'finland': 30, 'sweden': 30, 'norway': 20} ... }
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.
import csv from collections import defaultdict output = defaultdict(dict) with open('path/to/your.csv') as f: reader = csv.DictReader(f, delimiter=';') companies = reader.fieldnames[1:] for line in reader: country = line['country'] for company in companies: output[company][country] = line[company] # or output.setdefault(company, {})[country] = line[company] # if you want 'output' to be a "normal" dict instead of defaultdict print(dict(output)) # or just print(output) if you don't mind seeing OrderedDict # repr
outputs
{'company1': {'finland': '30', 'sweden': '20', 'norway': '10'}, 'company2': {'finland': '30', 'sweden': '30', 'norway': '20'}, 'company3': {'finland': '40', 'sweden': '50', 'norway': '70'}}