我有这样的表格,我想把它分组到字典中。我已导入,它采用逗号分隔格式
Accession,A,B,O,K,G,F
3364,+,-,+,-,+,-
3365,-+,-,+,-,+,-
3366,+,-,-,-,+,+
我想这样说:
{'3364': {'A': '+', 'B': '-', 'O': '+','K': '-', 'G': '+', 'F': '-'}}
我该怎么做?
答案 0 :(得分:0)
Jon是对的,但这里有一个提示:
d = {}
a = '3364,+,-,+,-,+,-'
n, *pm = a.split(',')
d[n] = {chr(i+65): v for i, v in enumerate(pm)}
print(d)
答案 1 :(得分:0)
这是Python 3.4的一种方式
import csv
from pprint import pprint
a_dict = {}
with open('csv_data.csv', newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='|')
header = csvreader.__next__()
header_keys = header[1:]
for row in csvreader:
a_dict[row[0]] = {}
key_value = list(zip(header_keys, row[1:]))
a_dict[row[0]].update(key_value)
pprint(a_dict)