The code works fine when there is a print instruction, but it causes an error when I comment on printing

I have a really weird mistake. I am using Python 2.7 for Linux Mint OS. Here is the code:

with open(edgeClusteringPath) as f: for line in f: clustId = str(currCluster) spl = line.split(" ") # print spl for i in spl: # print i (first, second) = edgeNodes[int(float(i))] nodeClusters.setdefault(first, set()) nodeClusters.setdefault(second, set()) nodeClusters[first].add(clustId) nodeClusters[second].add(clustId) # print i currCluster += 1 

edgeClusteringPath is the path to spatially separated data, and edgeNodes is a dictionary with the key = edgeId and value = (firstNode, secondNode). The problem arises in the second cycle. This code gives me an error:

 > line 34, in <module> (first, second) = edgeNodes[int(float(i))] KeyError: 0 

But when I uncomment one (or both) of the print i statement, the execution works fine (no errors). This is really strange, I don’t see how a print expression can affect the remaining code. I could uncomment the print expression, but I really don't need this print, so I'd rather get rid of this line.

Here is an example of edgNodes:

 87388: (3250, 6041), 87389: (3250, 6045), 87390: (3250, 6046) 

This is a huge dictionary, so I only extracted 3 key-value pairs. Thank you in advance!

Edit: Now my code is working. I had a problem with the paths I used. I used the wrong file to initialize the edgeNodes dictionary, and this caused problems. However, I posted the question only to find out if anyone knows how adding one line changes the behavior of the code. I have been using Java for 3 years and have never had a similar problem. I'm new to Python and don't know how it works internally, so I wanted to know if anyone had any idea about the effect of another code on one line.

Thank you for your opinions and suggestions!

+6
source share

All Articles