Home > front end >  How to create a doubly-nested dict from CSV?
How to create a doubly-nested dict from CSV?

Time:01-05

I am trying to read a Financial CSV with Python, and the data looks like this:

Company1;2018;12345;67890;
Company1;2019;34242;12313;
Company2;2018;12412;32423;
Company3;2017;12314;23554;
...

What I am searching for is a function that give me following result after reading this CSV:

Dict2 = {
   Company1: { 2018: { Costs: 123, employes: 1231}
               2019: { Costs: 231, employes: 1321}}
   Company2: { 2019: { Costs: 123, employes: 1231}
   Company3: { 2019: { Costs: 123, employes: 1231}
        }

I am processing the CSV like this:

file2 = open(pfad_ordner   "\daten\standortdaten\FirmenBilanz.csv", "r")

reader = csv.reader(file2, delimiter =";")

Dict2 = {}

for row in reader:
    Dict2[row[0]] = {"Jahr":row[2], "Ort":row[1], "Mitarbeiter_gewerblich": row[3]}

if i do it this way, python ignores the row with same company name. or lets better say, it updates the dictionary so only one row got stored per company key.

CodePudding user response:

The defaultdict class from the collections module can help you out.

You'll create Dict2 to be a dictionary that's meant to store other dictionaries:

Dict2 = defaultdict(dict)

Now, you can supply the company's name as a key, and give that key a value that is your "sub dict" of the year with the other values as a dictionary... all in one statement:

Dict2['Foo, inc.']['2018'] = {'Cost': 23, 'Employees': 9}

Here it is put together:

import csv
from collections import defaultdict

file2 = open(pfad_ordner   "\daten\standortdaten\FirmenBilanz.csv", "r")

reader = csv.reader(file2, delimiter =";")

Dict2 = defaultdict(dict)

for row in reader:
    name = row[0]
    year = row[1]
    Dict2[name][year] = { 'other values from row' }
  •  Tags:  
  • Related