你如何在Python中创建嵌套的dict? [英] How do you create nested dict in Python?
问题描述
Device_Name GDN Device_Type Device_OS
这些也是数据文件中存在的列,需要处理。
数据文件包含Data_Name列的数据填充&休息3列空白。映射文件包含所有列填充。我想要我的Python代码打开这两个文件,并且数据文件中的每个设备名称映射其GDN,Device_Type&来自映射文件的Device_OS值。
我知道只有2列存在时才使用dict(1需要映射),但是我不知道如何完成这需要3列需要映射。
以下是我尝试完成 Device_Type
的映射的代码:
x = dict([])
with open(Pricing Mapping_2013-04-22.csv rb)as in_file1:
file_map = csv.reader(in_file1,delimiter =',')
for 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 csv.reader中的行(in_file2,delimiter =','):
try:
row [27] = x [row [11]]
除了KeyError:
row [27] =
writer.writerow(row)
它返回 Atribute Error
。
经过一些研究,我意识到我需要创建一个嵌套的dict,但我不知道如何做到这一点。
请帮助我解决这个问题,或者以正确的方向推动我解决这个问题。
嵌套字典是字典中的字典。一个很简单的事情。
>>> d = {}
>>> d ['dict1'] = {}
>>> d ['dict1'] ['innerkey'] ='value'
>>> d
{'dict1':{'innerkey':'value'}}
你也可以使用 defaultdict
从 集合
包以方便创建嵌套字典。
>>>导入集合
>>> d = collections.defaultdict(dict)
>>> d ['dict1'] ['innerkey'] ='value'
>>> d#当前是一个defaultdict类型
defaultdict(< type'dict'> {'dict1':{'innerkey':'value'}})
>>> dict(d)#但完全像一个正常的字典。
{'dict1':{'innerkey':'value'}}
你可以填充,但是你想要的。
我会在您的代码中推荐如下:
d = {}#可以使用defaultdict(dict)代替
在file_map中的行:
#从某些东西
#导出行键,当使用defaultdict时,我们可以跳过下一步在row_key上创建一个字典
d [row_key] = {}
for idx,col in enumerate(行):
d [row_key] [idx] = col
根据您的评论:
可能上面的代码是混淆的问题。我的问题简而言之:我
有2个文件a.csv b.csv,a.csv有4列i j k l,b.csv也有
这些列。我是这些csvs的关键列。 j k l列
在a.csv中为空,但填充在b.csv中。我想使用'i`作为b.csv的键列映射到a.csv文件的jk
l列的值
我的建议是这样的东西,如这个(不使用defaultdict):
a_file =path /到/ a.csv
b_file =path / to / b.csv
#从文件a.csv
中打开(a_file)为f:
$跳过标题
f.next()
#获取第一个列作为键
keys =(line.split(',')[0] for f)
#创建空字典:
d = {}
#从文件b.csv
中打开(b_file)为f:
#标题除了第一个键头
headers = f.next()。split(',')[1:]
#iterate lines
for f中的行:
# colums
cols = line.strip()。split(',')
#检查以确保应该映射此键。
如果cols [0]不在键中:
continue
#添加键到dict
d [cols [0]] = dict(
#inner keys是标题名称,值为列
(头[idx],v)为idx,v为枚举(cols [1:]))
请注意,对于解析csv文件,有一个 csv模块。
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(<type 'dict'>, {'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.
这篇关于你如何在Python中创建嵌套的dict?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!