I have a file like:
HP;1<br/>
MN;stringval<br/>
IS/VD1;32.00<br/>
IS/HD1;1.34<br/>
IS/E/ID;362.0000000<br/>
IS/OLVD;0.0000000<br/>
ISN/I/HFD;3283.8999023<br/>
D1/I/MF;7.3059464<br/>
D1/I/P;32.0388412<br/>
D1/E/E;2778.4829102<br/>
D1/S1/SB/I/P;32.0388412<br/>
EN;5145<br/>
After the ";" there is a value that I want to put in the dictionary.
Every word that follows /is a key.
My ultimate goal is to have the following dictionary:
{"HP":1.0,"MN":'stringval',"IS":{"VD1":32.00,"HD1":1.34,"E":{"ID":362.0000000},"OLVD":0.0000000},"ISN":{"I":{"HFD":3283.8999023}},"D1":{"I":{"MF":7.3059464,"P":32.03884},"E":{"E":2778.4829102},"S1":{"SB":{"I":{"P":32.0388412}}}},"EN":5145}
I do not know the maximum number of levels of the final dictionary.
I tried the following code:
fid = open(myfile,mode='r')
content=fid.readlines()
content = [x.replace(' ','').strip() for x in content]
outdata = dict()
for i in content:
dict_struct = i.split(';')[0]
val = i.split(';')[-1]
n_lev = dict_struct.count('/')
test2 = '{"' + dict_struct.replace('/', '":{"') + '":' + val + '}' * (n_lev + 1)
try:
outdata.update(eval(test2))
print(test2)
except:
test2 = '{"' + dict_struct.replace('/', '":{"') + '":"' + val + '"' + '}' * (n_lev + 1)
print(test2)
outdata.update(eval(test2))
This code does not work because it update()actually overwrites some of the inputs; this is the conclusion:
{"HP": 1, "MN": "stringval", "IS": {"OLVD": 0.0}, "ISN": {"I": {"HFD": 3283.8999023}}, "D1": {"S1": {"SB": {"I": {"P": 32.0388412}}}}, "EN": 5145}
Could you give me some advice?