My while closing before it really should

0

I made a code that should take the number of rows from a txt file and then use this same number of rows in a while to convert the columns and rows to 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'm working on, the file has 551 lines, but the code always exits when ln reaches 275

    
asked by anonymous 25.08.2017 / 01:58

2 answers

0

You are using the same open dataset in the while, and it has already been consumed until the EOF is on, reaching the end of the file, if you give dataset.close () and give it open in the dataset again before the while, the your tie will not break.

    
08.09.2017 / 16:08
0

Place a

  

print ln

In the fibal of the while loop to check if it is not really doing all the iterations or if it is getting into that your if not and is not generating the result you expect.

    
09.09.2017 / 16:26