python dict嵌套dict_你如何在Python中创建嵌套的dict?

I have 2 csv files. First one is data file and other one is mapping file. Mapping file has 4 columns Device_Name GDN Device_Type Device_OS

These are also the columns which are present in data file and need to be worked upon.

Data file contains data with Device_Name column populated & rest 3 columns blank. Mapping file contains all columns populated. I want my Python code to open both files and for each device name in data file map its GDN, Device_Type & Device_OS value from mapping file.

I know how to use dict when only 2 columns are present (1 is needed to be mapped) but I don't know how to accomplish this when 3 columns need to be mapped.

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.

解决方案

A nested dict is a dictionary within a dictionary. A very simple thing.

>>> 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(, {'dict1': {'innerkey': 'value'}})

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

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

You can populate that however you want.

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:

may be 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.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值