我有一个具有以下格式的 csv 文件:
#ID #Number #Date #Name #Email
1978 26 24/4/10 Jim [email protected]
1328 31 22/7/10 Jim [email protected]
1908 26 21/4/10 Jim [email protected]
1918 26 29/4/10 Jim [email protected]
1938 46 24/4/10 Jim [email protected]
我已经打开了 csv 文件并将其打印出来。
我现在想把它做成字典,比如:
[ID:1978,号码:26,日期:24/4/10,姓名:Jim,电子邮件:
[email protected]]、[等]、[等]
我知道这可能很容易,但我是新手,已经被困了几个小时。
最佳答案
这是一些用纯 python 编写的代码,可以解决问题:
for line in file_contents_2:
line_contents = line.strip().split(",") # Removes the \n,
# then turns the line into a list, where each value is seperated
# by the comma
the_dictionary = {}
reference = ["ORIN","DEST","HORIZ","BEAR"]
for i in range(4): # iterates i=0 to i=3
# Arrays start at 0, so a=[1,2,3]; a[1] would return 2
the_dictionary[reference[i]] = line_contents[i]
dictionary_list.append(the_dictionary)
关于python - 如何基于 CSV 文件创建字典,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52826442/