Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (20.3k points)

Following is the code using which I tried to accomplish mapping of Device_Type:

x = dict([])

with open("Pricing Mapping_2013-04-22.csv", "rb") as in_file1:

    file_map = csv.reader(in_file1, delimiter=',')

    for row in file_map:

       typemap = [row[0],row[2]]

       x.append(typemap)

with open("Pricing_Updated_Cleaned.csv", "rb") as in_file2, open("Data Scraper_GDN.csv", "wb") as out_file:

    writer = csv.writer(out_file, delimiter=',')

    for row in csv.reader(in_file2, delimiter=','):

         try:

              row[27] = x[row[11]]

         except KeyError:

              row[27] = ""

         writer.writerow(row)

It returns the Atribute Error.

After some researching, I realized that I need to create a nested dict, but I don't have any idea on how to do this. Please help me in resolving this or nudge me in the right direction to resolve this.

2 Answers

0 votes
by (40.7k points)

A nested dict is a dictionary within a dictionary. Like this:

>>> d = {}

>>> d['dict1'] = {}

>>> d['dict1']['innerkey'] = 'value'

>>> d

{'dict1': {'innerkey': 'value'}}

You can also use a defaultdict from the collections package to facilitate creating nested dictionaries:

>>> import collections

>>> d = collections.defaultdict(dict)

>>> d['dict1']['innerkey'] = 'value'

>>> d  # currently a defaultdict type

defaultdict(<type 'dict'>, {'dict1': {'innerkey': 'value'}})

>>> dict(d)  # but is exactly like a normal dictionary.

{'dict1': {'innerkey': 'value'}}

You can populate that however, you want. Still, I would recommend in your code something like the following:

d = {}  # can use defaultdict(dict) instead

for row in file_map:

    # derive row key from something 

    # when using defaultdict, we can skip the next step creating a dictionary on row_key

    d[row_key] = {} 

    for idx, col in enumerate(row):

        d[row_key][idx] = col

According to your comment:

maybe the above code is confusing the question. My problem in nutshell: I have 2 files a.csv b.csv, a.csv has 4 columns i j k l, b.csv also has these columns. i is kind of key columns for these csvs'. j k l column is empty in a.csv but populated in b.csv. I want to map values of j k l columns using 'i` as key column from b.csv to a.csv file

My suggestion would be something like this (without using defaultdict):

a_file = "path/to/a.csv"

b_file = "path/to/b.csv"

# read from file a.csv

with open(a_file) as f:

    # skip headers

    f.next()

    # get first colum as keys

    keys = (line.split(',')[0] for line in f) 

# create empty dictionary:

d = {}

# read from file b.csv

with open(b_file) as f:

    # gather headers except first key header

    headers = f.next().split(',')[1:]

    # iterate lines

    for line in f:

        # gather the colums

        cols = line.strip().split(',')

        # check to make sure this key should be mapped.

        if cols[0] not in keys:

            continue

        # add key to dict

        d[cols[0]] = dict(

            # inner keys are the header names, values are columns

            (headers[idx], v) for idx, v in enumerate(cols[1:]))

Please note though, that for parsing csv files there is a csv module.

0 votes
by (106k points)

You can use the below-mentioned code to create a nested dictionary:-

from collections import defaultdict 

target_dict = defaultdict(dict) 

target_dict[key1][key2] = val

Related questions

Browse Categories

...