Skip to content Skip to sidebar Skip to footer

Python 2.7 Mixing Iteration And Read Methods Would Lose Data

I have an issue with a bit of code that works in Python 3, but fail in 2.7. I have the following part of code: def getDimensions(file,log): noStations = 0 noSpanPts = 0 dataSet

Solution 1:

The problem is that you are using next and readline on the same file. As the docs say:

. As a consequence of using a read-ahead buffer, combining next() with other file methods (like readline()) does not work right.

The fix is trivial: replace next with readline.

Solution 2:

If you want a short code to do that try:

lines = []
withopen(filename) as f:
    lines = [line for line in f if line.strip()]

Then you can do tests for lines.

Post a Comment for "Python 2.7 Mixing Iteration And Read Methods Would Lose Data"