简体   繁体   English

将csv中的字符串解析为字典

[英]Parsing string in csv into dictionary

I have a csv file in the following format 我有一个以下格式的csv文件

mod, id
128, 2pmk|5svq|3ar7|5xw6|5ncq|5a3s|2gvd|1i5d
574, 3zjt
0A, 4wb3|4wb2|4r8i
0C, 1r3o|4wb3|4wb2|2gq6|2gq4|2gq5|4r8i|2gpm|2g32|2gq7
0G, 1r3o|4wb3|4wb2|2gq6|2gq4|2gq5|4r8i|2gpm|2g32|2gq7
0U, 1r3o|4wb3|4wb2|2gq6|2gq4|2gq5|4r8i|2gpm|2g32|2gq7

I wanted to convert the information into a dictionary of key and values where the key would be id's [from a separate list] and values would be all the mod present in the id. 我想将信息转换为键和值的字典,其中键是id [来自单独的列表],值将是id中存在的所有mod。 I've written the following code which I think is wrong 我编写了以下代码,我认为这是错误的

import csv

id_list = ['1r3o', '4wb2', '1kmk']

n = {}

with open('test6.csv', mode='rU') as infile:
    reader = csv.reader(infile)
    for elem1 in id_list:
        for row in reader:
            identifier = row[1].split('|')
            for elem2 in identifier:
                while elem1 == elem2:
                    n[elem1] = row[0]

print n 

If there is no mapping between the id from the list and mod, I want the string 'None' appended to the dictionary value. 如果列表中的id和mod之间没有映射,我希望字符串值附加字符串“None”。 The desired output is shown below: 所需的输出如下所示:

{
'4wb2': ['OA', 'OC', 'OG', 'OU'],
'1r3o': ['OC', 'OG', 'OU'],
'1kmk': ['None']
}

Any help is appreciated. 任何帮助表示赞赏。 Thank you 谢谢

import csv

id_list = ['1r3o', '4wb2', '1kmk']

n = {}

mapping = {}
with open('test6.csv', mode='rU') as infile:
    reader = csv.reader(infile)
    for row in reader:
        mod, ids = row
        for id in ids.split('|'):
            if id not in mapping.keys():
                mapping[id] = set()
            mapping[id].add(mod)

for id in id_list:
    values = list(mapping.get(id, []))
    if not values:
        values = ['None']
    n[id] = values

print n 

I know this question already has an accepted answer, but I would like to share with you another approach using dictionary comprehensions and lambdas. 我知道这个问题已经有了一个公认的答案,但我想与你分享另一种使用字典理解和lambdas的方法。

import csv
id_list = ['1r3o', '4wb2', '1kmk', 'foo', 'bar', '3zjt']

# Read the content of the file
csv_content = []
with open('test6.csv', mode='rU') as file:
    for row in csv.reader(file):
        csv_content.append([row[0], row[1]])

# Collect the required data
mapped = { id: map(lambda f: f[0], filter(lambda r: id in r[1], csv_content)) for id in id_list }


# Add 'None' on empty results
results = dict(map(lambda item: (item[0], ['None'] if len(item[1]) == 0 else item[1]), mapped.iteritems()))

print(results)

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

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