我想将下面的数据转换为一个字典,第一列作为 ID 号,其余元素作为值
EDF768, Bill Meyer, 2456, Vet_Parking
TY5678, Jane Miller, 8987, AgHort_Parking
GEF123, Jill Black, 3456, Creche_Parking
到目前为止,我已经打开了文件并将数据转换为列表。但我无法将其转换为字典,因为它说明序列太长。
def createNameDict():
file = open("C:\Users\user\Desktop\\ParkingData_Part2.txt","rt")
contents = file.read()
print contents,"\n"
data_list = [line.split(",") for line in contents.split("\n")]
print data_list, "\n"
dict_list = []
data_dict = {}
for line in data_list:
key = line[0]
value = (line[1], line[2], line[3])
data_dict[key] = value
data_dict = dict()
dict_list.append(data_dict)
# start dictionary over
#print data_dict
print (data_list)
file.close()
我的输出是:
[['EDF768', ' Bill Meyer', ' 2456', ' Vet_Parking'],
['TY5678', ' Jane Miller', ' 8987', ' AgHort_Parking'],
['GEF123', ' Jill Black', ' 3456', ' Creche_Parking'],
['ABC234', ' Fred Greenside', ' 2345', ' AgHort_Parking'],
['GH7682', ' Clara Hill', ' 7689', ' AgHort_Parking'],
['JU9807', ' Jacky Blair', ' 7867', ' Vet_Parking'],
['KLOI98', ' Martha Miller', ' 4563', ' Vet_Parking'],
['ADF645', ' Cloe Freckle', ' 6789', ' Vet_Parking'],
['DF7800', ' Jacko Frizzle', ' 4532', ' Creche_Parking'],
['WER546', ' Olga Grey', ' 9898', ' Creche_Parking'],
['HUY768', ' Wilbur Matty', ' 8912', ' Creche_Parking']]
有人可以给我提示将上面的列表转换为字典,例如:
{'EDF768': ' Bill Meyer', ' 2456', ' Vet_Parking'}