This is a simplified version of my code. Basically I have a file of general format:
2 3 5
4 8 9
etc
for n lines, and I must organize the file into a dictionary. Before this, I need a sum of one of the data points, so I iterate through the file to create a sum first, then iterate back through each line again in a second for suite to create a nested dictionary data structure. But for some reason the second suite is completely ignored.
This is a simplified version, so obviously some things could be moved around, but in the real file I really do need two separate loops because one of the values is a percentage based on the total sum in the file which is only fully defined after the end of the first for loop.
file = open('file.txt', 'r')
dict = {}
for line in file:
v1, v2, v3 = file.split(' ')
sum += v1
for line in file:
v1, v2, v3 = file.split(' ')
key = v1
subkey = v2
ratio = v3/sum
value = ratio
dict[key] = [subkey],[value]
print(dict)
For some reason this just gives
{}
Which is taking the original assignment of the dictionary and ignoring the second for iteration. If I try to print anything in the second suite, like just v1, it is ignored. If I put it in the first for
loop, it works.