My while closes before what really should

Asked

Viewed 82 times

1

I made a code that should take the number of rows of a txt file and then use this same number of rows in a while to convert columns and rows into array.

lines = 0
ln = 0

for line in dataset:
    line = line.strip()
    lines += 1
    print (lines, ln)

while ln < (lines):
    line1 = dataset.readline()
    if not line1: break
    words = str.split(line1)
    ln += 1

In the case I am working, the file has 551 lines, but the code always ends when the ln reaches 275

  • 2

    Have you seen what’s on line 275? It may be that the content is making the expression true if not line1: break try to use for ln in range(lines) instead of while

2 answers

0

Put a

print in

in the fibal of the while loop to check if he really isn’t doing all the iterations or if he’s entering this if not and isn’t generating the result you expect.

0

You are using the same open dataset at while, and it has already been consumed by EOF no for, reaching the end of the file, if you give dataset.close() and open the dataset again before while, your loop will not enter break.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.