Problem reading in data

The following code fragment reads in lines of comma-separated numbers, converts each line to a list of int’s and puts them in another list. Each line is terminated with a \n which has to be dealt with:

file = open("filename data.txt", "r")
grid = []

for line in file:
    try:
        lineval = [int(line)]               
        temp = [n for n in lineval]
    except ValueError:
        break
    grid.append(temp)

I’m only getting the first line:

>>> grid[:4]
[[59]]                  # remaining lines not read

If I omit the int statement and omit the try/except test I get \n’s, which of course I don’t want because they preclude the int statement, but I do get all the lines, eg:

>>> grid[:4]
[['59\n'], ['73,41\n'], ['52,40,09\n'], ['26,53,06,34\n']]   # all lines read but still as strings + \n's

How do I get the "for’ loop to finish reading the rest of the lines in the file?

A second question would be why some people’s solutions don’t require the exception for the newlines. It would appear they’re either not bringing in the newlines or somehow able to get rid of them or ignore them… I raised that question previously and it was suggested that I might be dealing with blank lines and they weren’t, but that is not the case. We’re all using the same text file; we all have to deal with \n’s; there are no blank lines. But anyway, I’m resigned to letting that remain a mystery for now – I’ll be happy to get this “for” to finish its looping.

Tackling the second question first, that’s because int() doesn’t care about whitespace. Try it out in the interpreter:

rhodri@scrote:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int(" 123\n")
123
>>> 

A little thought should suggest to you that if int() doesn’t care about whitespace, something else must be causing it to raise a ValueError. What else is in your input file that isn’t a digit or a newline?

If your answer isn’t “the commas”, you need to take a break :slight_smile: