简体   繁体   English

从 csv 文件创建字典

[英]Create dictionary from a csv file

I have a csv file which looks like this after executing the following code:执行以下代码后,我有一个 csv 文件,如下所示:

with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
    print(row)

Output:输出:

OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')]) OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')]) OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), ('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])

How do I create a dictionary that looks like this:我如何创建一个看起来像这样的字典:

{
 '0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), 
 '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'), 
 '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')
 }

My code is :我的代码是:

d = {}
with open('XYZ.csv') as csvfile:
reader = csv.DictReader(csvfile)
for i in reader:
    for j in i.keys():
        if j in cols:
            d[i['img_id']] = i[j]
print(d)

This is yielding me:这让我产生:

{'0359a': '2', '0577a': '2', '1120a': '2'}

How do I avoid this overwriting?我如何避免这种覆盖?

This is possible with a simple dictionary comprehension as follows (see explanation in comments):这可以通过如下简单的字典理解来实现(请参阅注释中的解释):

lines = [
    OrderedDict([('', '0'), ('img_id', '0359a'), ('f1', '2'), ('f2', '1'), ('f3', '1'), 
    ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')]),
    OrderedDict([('', '1'), ('img_id', '0577a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
    ('f4', '0'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '1'), ('f9', '2')]),
    OrderedDict([('', '2'), ('img_id', '1120a'), ('f1', '2'), ('f2', '1'), ('f3', '1'),
    ('f4', '3'), ('f5', '2'), ('f6', '2'), ('f7', '0'), ('f8', '2'), ('f9', '2')])]

# for our new dictionary
# key is img_id value in OrderedDict
# and value is a list of all values in OrderedDict if their key isn't '' or 'img_id'
d = {l['img_id']: tuple([v for k, v in l.items() if k not in ('', 'img_id')]) for l in lines}
print(d)

This gives us:这给了我们:

{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), 
 '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2'), 
 '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2')}

you could use a defaultdict with each key being ad[i['img_id']] and the value being a list that you keep appending to您可以使用每个键为 ad[i['img_id']] 的defaultdict并且值是您不断附加到的列表

    from collections import defaultdict
    d = defaultdict(list)
    ...
    d[i['img_id']].append(i[j])

You can use the following dict comprehension that unpacks the keys and values from the dict items:您可以使用以下 dict 理解来解包 dict 项目中的键和值:

{k: tuple(i for _, i in v) for _, (_, k), *v in (d.items() for d in reader)}

This returns:这将返回:

{'0359a': ('2', '1', '1', '0', '2', '2', '0', '2', '2'), '0577a': ('2', '1', '1', '0', '2', '2', '0', '1', '2'), '1120a': ('2', '1', '1', '3', '2', '2', '0', '2', '2')}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM