在Python中从csv文件创建嵌套字典

2024-07-07 06:27:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我从CSV文件中读取信息,并使用嵌套字典来映射文件中的重复信息。如何为该文件的所有行创建嵌套词典?数据示例(不是实际数据,而是基本相同的概念)

State ,City/Region ,Questions ,Answers 
NY,Manhattan ,East/West Coast? ,East 
NY,Manhattan ,been there? ,yes
NY,Brooklyn ,East/West Coast? ,East 
NY,Brooklyn ,been there? ,yes
NY,Brooklyn ,Been to coney island? ,yes
NY,Queens ,East/West Coast? ,East 
NY,Queens ,been there? ,yes
NY ,Staten Island ,is island? ,yes
MA,Boston ,East/West Coast? ,East 
MA,Boston ,like it there? ,yes
MA,Pioneer Valley ,East/West Coast? ,East 
MA,Pioneer Valley ,city? ,no
MA,Pioneer Valley ,college town? ,yes
CA,Bay Area ,warm? ,yes
CA ,Bay Area ,East/West Coast? ,West 
CA ,SoCal ,north or south? ,south 
CA ,SoCal ,warm ,yes 

因此,基本上,主字典有3个键:NY、MA、CA,每个键都有一个以City/Region为键的字典,每个City/Region都有问题和答案。
所以这将是一个非常嵌套的字典,但我无法找出语法,以便对文件中的每一行都这样做。在

我尝试过打开文件,使用for循环来读取行并用“,”分隔行。像这样:

^{pr2}$

Tags: 文件city字典regionyescatherewest
3条回答
import csv
from collections import defaultdict
from functools import partial

defaultdict_of_dict = partial(defaultdict, dict)
master = defaultdict(defaultdict_of_dict)

with open("data.txt", 'r') as f:
    csv_reader = csv.reader(f)
    next(csv_reader)  # Skip the first line
    for row in csv_reader:
        state, city, question, answer = [field.strip() for field in row]
        master[state][city][question] = answer


print(master['NY']['Queens'])
# {'been there?': 'yes', 'East/West Coast?': 'East'}
print(master['NY']['Queens']['been there?'])
# yes

您可以使用负责分割的csv模块读取CSV文件。在

您给出的示例数据充满了不需要的空格。如果您的实际数据与此相同,我们使用strip对其进行清理。在

为了避免在字典中创建丢失的键,可以使用defaultdict。它使用默认值动态创建缺少的键。在

例如,您可以:

^{pr2}$

要创建一个defaultdict,将空dict作为丢失键的默认值,并按如下方式使用它:

d["new_key"]["subkey"] = 5
print(d)
# defaultdict(<class 'dict'>, {'new_key': {'subkey': 5}})

在您的例子中有一个困难:您需要一个嵌套字典,所以我们需要一个defaultdictdefaultdict,属于{}

我们给defaultdict的参数必须是可调用的,因此我们不能编写类似defaultdict(defaultdict(dict))的内容,因为defaultdict(dict)defaultdict,而不是函数。实现这一点的一种方法是使用functools.partial创建一个defaultdict_of_dict函数,我们可以将它传递给主defaultdict。在

你可以试试这个稍短的版本:

f = open(myfile).readlines()

f = [i.strip('\n').split(',') for i in f]

d = {i[0]:{i[1]:[]} for i in f[1:]}

for i in f[1:]:
    if i[1] not in d[i[0]]:
        d[i[0]][i[1]] = i[2:]
    else:
        d[i[0]][i[1]].extend(i[2:])

print d

我想办法让它工作。在

import pprint 
MasterDict={}
    my_file.readline()
    for line in my_file:
        line=line.split(",")
        if line[0] not in MasterDict:
            MasterDict[line[0]] = {}
        if line[1]:
            if line[1] not in MasterDict[line[0]]:
                MasterDict[line[0]][line[1]] = []
            MasterDict[line[0]][line[1]].append((line[2], line[3]))
    pp = pprint.PrettyPrinter(indent=4)
    pp.pprint(MasterDict)

相关问题 更多 >