I'm trying to create a dictionary object like below using the input file data structure as below, During conversion inner object is being replicated. Any advise what fix is needed for desire output
input file data:/home/file1.txt
[student1]
fname : Harry
lname : Hoit
age : 22
[Student2]
fname : Adam
lname : Re
age : 25
expected output :
{'Student1' : {'fname' : 'Harry', 'lname' : 'Hoit', 'Age' : 22},
'Student2' : {'fname' : 'Adam', 'lname' : 'Re', 'Age' : 25}}
def dict_val():
out = {}
inn = {}
path= '/home/file1.txt'
with open(path, 'r') as f:
for row in f:
row = row.strip()
if row.startswith("["):
i = row[1:-1]
# inn.clear() ## tried to clean the inner loop during second but its not correct
else:
if len(row) < 2:
pass
else:
key, value = row.split('=')
inn[key.strip()] = value.strip()
out[i] = inn
return out
print(dict_val())
current output: getting duplicate during second iteration
{'student1': {'fname': 'Adam', 'lname': 'Re', 'age': '25'},
'Student2': {'fname': 'Adam', 'lname': 'Re', 'age': '25'}}