【问题标题】:python import csv listspython导入csv列表
【发布时间】:2012-12-31 13:36:51
【问题描述】:

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

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

【问题讨论】:

    标签: python csv dictionary import


    【解决方案1】:

    这有帮助吗?

    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)
    

    【讨论】:

    • 这很有帮助。感谢您的帮助!
    【解决方案2】:

    使用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']}}
    

    【讨论】:

    • 这是一个干净的实现。
    • 感谢您提供 python3 示例。效果很好!
    猜你喜欢
    • 2014-08-31
    • 2022-01-03
    • 2018-05-06
    • 2013-11-04
    • 2015-07-06
    • 1970-01-01
    • 2015-07-23
    相关资源
    最近更新 更多