I’m currently reading values from file and spliting them as parameter and value e.g. @id=7 becomes param = @id, value = 7. I would like to use the param variable as a new key in the dictionary. However, it is not working as expected. I’m using the following code.
list1 = {}
with open('C:/Temp/file1.txt') as f:
lines = f.read().splitlines()
for line in lines:
middle = line.find("=")
param = line[:middle]
value = line[middle+1:]
list1[param] = value
In this code, the dictionary key and value becomes 7.
Thanks in advance.
If you are defining list1 as a dict
list1 = {}then your print statement is incorrect.both
list1[param]andvaluewould be 7. sincelist1[param]would give you the value of it’s contents and not it’s key.Try looking at the dictionary afterwards by printing it.