我试图创建一个嵌套的if函数,用户名是主字符串,其他是子字符串。但由于某些原因,它没有分隔行,而是创建了多个嵌套的字典,而现在它只是卡在文本文件的第一行。请帮助
MY TEXT FILE
shanm = null|Shanmugaraja|09/04/2002|0149606345|020409140817|0102393345|4770 4848 0109 0294
jiken = null|Soo Jiken|08/06/2000|0149600239|020908140213|011349780|8900 2828 1129 0889
嵌套字典的代码
with open("something.txt", 'r') as f:
data_dict = {}
data_dict2 = {}
data3 = {}
for line in f:
f.read()
k, v = line.strip().split("=")
listDetails = line.strip().split('|')
data_dict = {"Full Name": listDetails[1]}
data_dict.update({"Date of Birth": listDetails[2]})
data_dict.update({"Phone Number": listDetails[3]})
data_dict.update({"Identification Card (IC) Number": listDetails[4]})
data_dict.update({"Emergency Contact Number": listDetails[5]})
data_dict.update({"Credit /Debit Card Details ": listDetails[6]})
data3[k] = data_dict
print(data3)
所需输出
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 4848 0109 0294'}}
{'jiken ': {'Full Name': 'Soo Ji', 'Date of Birth': '08/06/2000', 'Phone Number': '0149600239', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
输出了
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 4848 0109 0294'}}
#NOT READING THE NEXT PART OF THE LINE
OR (WITHOUT "F.READ()">
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 2828 0109 0394'}}
{'shanm ': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0102393345', 'Credit /Debit Card Details ': '4770 2828 0109 0394'}, 'jiken ': {'Full Name': 'Soo Jiken', 'Date of Birth': '08/06/2000', 'Phone Number': '0149600239', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
重新读取所有内容
试着这样更新你的代码:
with open("something.txt", 'r') as f:
data_dict = {}
data3 = {}
for line in f.readlines():
k, v = line.strip().split("=")
listDetails = line.strip().split('|')
data_dict = {"Full Name": listDetails[1]}
data_dict.update({"Date of Birth": listDetails[2]})
data_dict.update({"Phone Number": listDetails[3]})
data_dict.update({"Identification Card (IC) Number": listDetails[4]})
data_dict.update({"Emergency Contact Number": listDetails[5]})
data_dict.update({"Credit /Debit Card Details ": listDetails[6]})
data3[k] = data_dict
print(data3)
看起来您想要一个字典列表。如果是这样,您可以这样做:
keys = ['Full name', 'Date of Birth', 'Phone Number',
'Identification Card (IC) Number', 'Emergency Contact Number', 'Credit /Debit Card Details']
lod = []
with open('something.txt') as infile:
for line in infile:
key, v = line.split('=')
d = {}
for k, v in zip(keys, v.split('|')[1:]):
d[k] = v
lod.append({key.strip(): d})
for d in lod:
name = list(d.keys())[0]
print(d[name]['Full name']
keys = ["Full Name", "Date of Birth", "Phone Number",
"Identification Card (IC) Number", "Emergency Contact Number",
"Credit /Debit Card Details "]
data = {}
with open("something.txt", 'r') as f:
for line in f:
key, values = line.strip().split(" = ") # note the space around =, to avoid trailing space in key
values = values.split('|')
data[key] = dict(zip(keys, values[1:]))}
print(data)
print(data.get('jiken'))
可能你想把每个字典data
存储在某种容器类型中,比如list
。
更新:我编辑了我的代码来创建一个字典,并通过用户名访问每条记录。