I have successfully created a nested dictionary from a text file but now I want to call out a specific dict out of the nested ones with a variable
#Text file
shanm = null|Shanmugaraja|09/04/2002|0149606345|020409140817|0199999999|4870 2929 0108 2870
jiken = null|Soo Idiot Jiken|08/06/2000|0199999999|020908140213|011349780|8900 2828 1129 0889
keller = null|Mathew Keller|02/05/2002|0199999999|0203140819|019607892|9801 2828 5596 0889
#This is my code for the nested dictionary
keys = ["Full Name", "Date of Birth", "Phone Number",
"Identification Card (IC) Number", "Emergency Contact Number",
"Credit /Debit Card Details "]
username = "jiken"
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('|')
# print(key)
data2 = {key: dict(zip(keys, values[1:]))}
print(data2)
#output. It is working but I don't want to call out the entire dict instead I just want to call out the dict that starts with the name "jiken" but it's not working
{'shanm': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0199999999', 'Credit /Debit Card Details ': '4870 2929 0108 2870'}}
{'jiken': {'Full Name': 'Soo Idiot Jiken', 'Date of Birth': '08/06/2000', 'Phone Number': '0199999999', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
{'keller': {'Full Name': 'Mathew Keller', 'Date of Birth': '02/05/2002', 'Phone Number': '0199999999', 'Identification Card (IC) Number': '0203140819', 'Emergency Contact Number': '019607892', 'Credit /Debit Card Details ': '9801 2828 5596 0889'}}
#Desired output, I want it to just print the dict according to the username variable in this case, "jiken"
{'jiken': {'Full Name': 'Soo Idiot Jiken', 'Date of Birth': '08/06/2000', 'Phone Number': '0199999999', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
CodePudding user response:
Just check if key == username
and then create data2 out of it as below:
keys = ["Full Name", "Date of Birth", "Phone Number",
"Identification Card (IC) Number", "Emergency Contact Number",
"Credit /Debit Card Details "]
username = "jiken"
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('|')
# print(key)
if key == username:
data2 = {key: dict(zip(keys, values[1:]))}
print(data2)
CodePudding user response:
You can try:
keys = ["Full Name", "Date of Birth", "Phone Number", "Identification Card (IC) Number", "Emergency Contact Number", "Credit /Debit Card Details "]
data2 = {}
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('|')
data2[key] = dict(zip(keys, values[1:]))
# Here, you can add any username and take respective data.
username = "jiken"
print(data2[username])
username = "shanm"
print(data2[username])
CodePudding user response:
You can define data2
as list
then append dict
to list
then define search function and always you can search any username
like below:
>>> data2 = []
>>> with open("something.txt", 'r') as f:
... for line in f:
... key, values = line.strip().split(" = ")
... values = values.split('|')
... data2.append({key: dict(zip(keys, values[1:]))})
# after read file you have this data2
>>> data2 = [{'shanm': {'Full Name': 'Shanmugaraja', 'Date of Birth': '09/04/2002', 'Phone Number': '0149606345', 'Identification Card (IC) Number': '020409140817', 'Emergency Contact Number': '0199999999', 'Credit /Debit Card Details ': '4870 2929 0108 2870'}},
... {'jiken': {'Full Name': 'Soo Idiot Jiken', 'Date of Birth': '08/06/2000', 'Phone Number': '0199999999', 'Identification Card (IC) Number': '020908140213', 'Emergency Contact Number': '011349780', 'Credit /Debit Card Details ': '8900 2828 1129 0889'}},
... {'keller': {'Full Name': 'Mathew Keller', 'Date of Birth': '02/05/2002', 'Phone Number': '0199999999', 'Identification Card (IC) Number': '0203140819', 'Emergency Contact Number': '019607892', 'Credit /Debit Card Details ': '9801 2828 5596 0889'}}]
>>> def ret_dct_user(dict_username , username):
... for dct in dict_username:
... if username in dct:
... return dct
Output:
>>> ret_dct_user(data2, 'jiken')
{'jiken': {'Full Name': 'Soo Idiot Jiken',
'Date of Birth': '08/06/2000',
'Phone Number': '0199999999',
'Identification Card (IC) Number': '020908140213',
'Emergency Contact Number': '011349780',
'Credit /Debit Card Details ': '8900 2828 1129 0889'}}
>>> ret_dct_user(data2, 'shanm')
{'shanm': {'Full Name': 'Shanmugaraja',
'Date of Birth': '09/04/2002',
'Phone Number': '0149606345',
'Identification Card (IC) Number': '020409140817',
'Emergency Contact Number': '0199999999',
'Credit /Debit Card Details ': '4870 2929 0108 2870'}}