Skip to content
Advertisement

How to create a two-level-dictionary from one file?

I have a csv file that looks like this (in reality it is bigger):

JavaScript

How could I read this file the easiest, so that I could get a dictionary like this (dictionaries inside a dictionary):

JavaScript

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

outputs

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement