I'm trying to create a nested if function with the username being the main string and the others being the substring. But for some reason, it does not separate the lines, creating multiple nested dictionaries instead right now it's just stuck in the first line of the text file. Plz help
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
MY CODE FOR NESTED DICTIONARY
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)
DESIRED OUTPUT
{'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'}}
OUTPUT RECEIVED
{'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'}}
READS EVERYTHING ALL OVER AGAIN
CodePudding user response:
try updating your code to this:
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)
CodePudding user response:
Looks like you want a list of dictionaries. If so, you could do it like this:
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']
CodePudding user response:
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'))
Probably you want to store each dict data
in some sort of container type like list
.
UPDATE: I edited my code to create a dict and access each record by username.