File to import contains 1000 rows of two five digit numbers with no delimiter apart from CR/LF
import re
with open('./Day1Data.txt') as Both:
line = Both.readline()
while line:
line = Both.readline()
L = list(filter(None, re.split('s+', line)))
A = L[0]
B = L[1]
print('L[0]: ' + A + 't' + ' / ' + 'L[1]: ' + B + 'n')
Complains:
A = L[0]
~^^^
IndexError: list index out of range.
After following suggestions to move the readLine
in the while
loop to the end, it successfully peeled off a line at a time, split it so L contains an array of two values, then A & B are the two values pulled out separately. All good!
import re
with open('./Day1Data.txt') as Both:
line = Both.readline()
while line:
L = list(filter(None, re.split('s+', line)))
A = L[0].strip()
B = L[1].strip()
print('L[0]: ' + A + ' / ' + 'L[1]: ' + B + 'n')
line = Both.readline()
Which worked, giving expected output
L[0]: 70055 / L[1]: 70208
L[0]: 50040 / L[1]: 21717
L[0]: 24733 / L[1]: 98815
L[0]: 32343 / L[1]: 36965
L[0]: 75319 / L[1]: 86047
L[0]: 99792 / L[1]: 57533
L[0]: 51677 / L[1]: 88962
L[0]: 69872 / L[1]: 53641
L[0]: 68258 / L[1]: 35233
L[0]: 72704 / L[1]: 20997
L[0]: 78327 / L[1]: 63698
L[0]: 62435 / L[1]: 56887
L[0]: 84014 / L[1]: 77208
L[0]: 51255 / L[1]: 53718
3