4

我想将 CSV 导入 python 中的多个字典

queryInclude,yahoo,value1
queryInclude,yahoo,value2
queryInclude,yahoo,value3
queryExclude,yahoo,value4
queryExclude,yahoo,value5
queryInclude,google,value6
queryExclude,google,value7

我的理想结果是 row[0]=dictionary、row[1]=key 和 row[2]=value 或值列表

queryInclude = {
        "yahoo": ["value1", "value2", "value3"],
        "google": ["value6"] }
queryExclude = {
        "yahoo": ["value4", "value5"],
        "google": ["value7"] }

这是我的代码:

import csv
queryList=[]
queryDict={}
with open('dictionary.csv') as csvfile:
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    for row in reader:
        queryDict[row[1]] = queryList.append(row[2])
        print queryDict

{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'yahoo': None}
{'google': None, 'yahoo': None}
{'google': None, 'yahoo': None}

如果需要,我可以灵活地更改 CSV 格式。我上面发布的理想结果是我已经硬编码到我的应用程序中的结果。我正在努力让未来添加更多价值变得更容易。我已经花了很多时间研究这个,如果我取得更多进展,我会继续更新。我的思维过程看起来像这样......不确定在迭代 CSV 行时如何构造我的循环和组合类似的值有多接近......

for row in reader:
    where row[0] = queryInclude:
        create a dictionary combining keys into a list of values
    where row[0] = queryExclude:
        create a dictionary combining keys into a list of values
4

2 回答 2

4

使用defaultdict可以防止必须考虑添加到字典中的第一个元素。它在键不存在时声明默认类型,并且必须是创建默认对象的可调用对象:

#! python3
import csv
from io import StringIO
from collections import defaultdict
from pprint import pprint

data = StringIO('''\
queryInclude,yahoo,value1
queryInclude,yahoo,value2
queryInclude,yahoo,value3
queryExclude,yahoo,value4
queryExclude,yahoo,value5
queryInclude,google,value6
queryExclude,google,value7
''')

D = defaultdict(lambda: defaultdict(list))
for d,k,v in csv.reader(data):
    D[d][k].append(v)
pprint(D)

输出:

{'queryExclude': {'google': ['value7'],
                  'yahoo': ['value4', 'value5']},
 'queryInclude': {'google': ['value6'],
                  'yahoo': ['value1', 'value2', 'value3']}}
于 2013-01-17T04:03:42.437 回答
2

这有帮助吗?

import StringIO
import csv

csvfile = StringIO.StringIO("""queryInclude,yahoo,value1
queryInclude,yahoo,value2
queryInclude,yahoo,value3
queryExclude,yahoo,value4
queryExclude,yahoo,value5
queryInclude,google,value6
queryExclude,google,value7""")

reader = csv.reader(csvfile, delimiter=',', quotechar='|')

dict1={}
for row in reader:
    key1, provider, value1 = row
    if not dict1.has_key(key1):
        dict1[key1] = {}
    if not dict1[key1].has_key(provider):
        dict1[key1][provider] = []
    dict1[key1][provider].append(value1)
于 2013-01-17T03:57:46.830 回答