I am trying to read ascii text file as a matrix. The ascii file has 512 entries which are treated as rows and after each breakline character, a new row starts. It looks something like
I tried using
with open('24_5.asc', 'r') as f:
l = [[int(num) for num in line.split(',')] for line in f]
print(l)
but that gives an error
invalid literal for int() with base 10: '\n'
Any help will be appreciated
CodePudding user response:
Your problem here is that when you split a line the last element of the list returned will be a string "\n"
, which cannot be converted to int. So Removing it with [:-1]
should work:
with open('24_5.asc', 'r') as f:
l = [[int(num) for num in line.split(',')[:-1]] for line in f]
print(l)