python导入csv列表

时间:2013-01-17 03:41:02

标签: python csv dictionary import

我想在CSV中将CSV导入多个词典

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

我的理想结果是行[0] =字典,行[1] =键,行[2] =值或值列表

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

2 个答案:

答案 0 :(得分: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']}}

答案 1 :(得分: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)