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.