我正在尝试从 python 中的 csv 文件制作字典,但我有多个类别。我希望键是 ID 号,值是项目的名称。这是文本文件:
"ID#","name","quantity","price"
"1","hello kitty","4","9999"
"2","rilakkuma","3","999"
"3","keroppi","5","1000"
"4","korilakkuma","6","699"
这就是我到目前为止所拥有的:
txt = open("hk.txt","rU")
file_data = txt.read()
lst = [] #first make a list, and then convert it into a dictionary.
for key in file_data:
k = key.split(",")
lst.append((k[0],k[1]))
dic = dict(lst)
print(dic)
不过,这只是打印一个空列表。我希望键是 ID#,然后值将是产品的名称。我将制作另一个字典,名称为键,ID# 为值,但我认为这将是同一件事,但反过来。