你如何在 Python 中创建嵌套的 dict? [英] How do you create nested dict in Python?
问题描述
我有 2 个 CSV 文件:数据"和映射":
- 'Mapping' 文件有 4 列:
Device_Name
、GDN
、Device_Type
和Device_OS
.所有四列都已填充. - 'Data' 文件具有这些相同的列,其中填充了
Device_Name
列,而其他三列为空. - 我希望我的 Python 代码打开这两个文件,并为数据文件中的每个
Device_Name
映射其GDN
、Device_Type
和 <映射文件中的 code>Device_OS 值.
当只有 2 列(需要映射 1 列)时,我知道如何使用 dict,但是当需要映射 3 列时,我不知道如何实现这一点.
以下是我尝试完成Device_Type
映射的代码:
x = dict([])使用 open("Pricing Mapping_2013-04-22.csv", "rb") 作为 in_file1:file_map = csv.reader(in_file1, delimiter=',')对于 file_map 中的行:typemap = [row[0],row[2]]x.append(typemap)将 open("Pricing_Updated_Cleaned.csv", "rb") 作为 in_file2,将 open("Data Scraper_GDN.csv", "wb") 作为 out_file:writer = csv.writer(out_file, delimiter=',')对于 csv.reader(in_file2, delimiter=',') 中的行:尝试:行[27] = x[行[11]]除了 KeyError:行[27] = ""writer.writerow(行)
返回属性错误
.
经过一番研究,我想我需要创建一个嵌套的字典,但我不知道如何做到这一点.
嵌套字典是字典中的字典.很简单的事情.
<预><代码>>>>d = {}>>>d['dict1'] = {}>>>d['dict1']['innerkey'] = '值'>>>d['dict1']['innerkey2'] = 'value2'>>>d{'dict1': {'innerkey': 'value', 'innerkey2': 'value2'}}您还可以使用 defaultdict
来自 collections
包以方便创建嵌套字典.
您可以随意填充.
我会在您的代码中推荐类似以下内容:
d = {} # 可以使用 defaultdict(dict) 代替对于 file_map 中的行:# 从某事派生行键# 使用defaultdict时,我们可以跳过下一步在row_key上创建字典d[row_key] = {}对于 idx,enumerate(row) 中的 col:d[row_key][idx] = col
根据您的评论:
<块引用>可能是上面的代码混淆了问题.我的问题简而言之:我有 2 个文件 a.csv b.csv,a.csv 有 4 列 i j k l,b.csv 也有这些列.i 是这些 csvs 的关键列.j k l 列在 a.csv 中为空,但在 b.csv 中填充.我想映射 j k 的值l 列使用 'i' 作为从 b.csv 到 a.csv 文件的关键列
我的建议是像这样的(不使用 defaultdict):
a_file = "path/to/a.csv";b_file = "path/to/b.csv";# 从文件 a.csv 中读取使用 open(a_file) 作为 f:# 跳过标题f.next()# 获取第一列作为键键 = (line.split(',')[0] for line in f)# 创建空字典:d = {}# 从文件 b.csv 读取使用 open(b_file) 作为 f:# 收集除第一个键标头以外的标头headers = f.next().split(',')[1:]# 迭代行对于 f 中的行:# 收集列cols = line.strip().split(',')# 检查以确保应该映射此键.如果 cols[0] 不在键中:继续# 给字典添加键d[cols[0]] = dict(# 内部键是标题名称,值是列(headers[idx], v) for idx, v in enumerate(cols[1:]))
请注意,解析 csv 文件有一个 csv 模块.
I have 2 CSV files: 'Data' and 'Mapping':
- 'Mapping' file has 4 columns:
Device_Name
,GDN
,Device_Type
, andDevice_OS
. All four columns are populated. - 'Data' file has these same columns, with
Device_Name
column populated and the other three columns blank. - I want my Python code to open both files and for each
Device_Name
in the Data file, map itsGDN
,Device_Type
, andDevice_OS
value from the 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 Attribute Error
.
After some researching, I think I need to create a nested dict, but I don't have any idea how to do this.
A nested dict is a dictionary within a dictionary. A very simple thing.
>>> d = {}
>>> d['dict1'] = {}
>>> d['dict1']['innerkey'] = 'value'
>>> d['dict1']['innerkey2'] = 'value2'
>>> d
{'dict1': {'innerkey': 'value', 'innerkey2': 'value2'}}
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屋!