The number of lines is known at the outset.
0 1 2 3 4 5 6 7 8
8 1 2 3 4 5 6 7 0
4 0 8 2 6 3 7 1 5
..n such lines
line1 = [0, 1, 2, 3, 4, 5, 6, 7, 8]
line2 = [8, 1, 2, 3, 4, 5, 6, 7, 0]
line3 = [4, 0, 8, 2, 6, 3, 7, 1, 5]
.
.
linen = [n1, ........ n9]
I'm currently:
#The lines start at the 7th byte in the input file.
f.seek(7)
#Getting rid of the '\r\n'
lines = [line.rstrip('\n\r') for line in f]
#1st line
line0 = lines[0]
line = [[int(i) for i in line0.split()]]
print line
...& so on for the 'n' lines
str.split()
already removes whitespace from the end, including a newline. There is no need to strip the \r
; Python already has translated the line separator to just \n
.
Don't try to assign to multiple line*
variables; just use a list instead:
with open(filename, 'r') as fobj:
all_lines = [[int(num) for num in line.split()] for line in fobj]
Now you have a list of lists with integers.
You could just process each line as you read it from the file; move towards the end product at that time rather than hold all lines in memory:
with open(filename, 'r') as fobj:
for line in fobj:
numbers = [int(num) for num in line.split()]
# do something with this line of numbers before moving on to the next.
Just split and map to int, split will do all the work for you:
with open("in.txt") as f:
for line in f:
print(map(int,line.split())) # list(map(int,line.split())) for py3
To get a list of lists use a list comprehension:
with open("in.txt") as f:
data = [map(int,line.split()) for line in f]
If you use python3 you need to use list(map...
as map returns and iterator in python3 vs a list in python2.
You could also use a dict to access each list by name/key but you can use indexing so a dict would be pointless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With