Reputation: 1
I was trying to extract even lines from a text file and output to a new file. But with my codes python warns me "list index out of range". Anyone can help me? THANKS~
Code:
f = open('input.txt', 'r')
i = 0
j = 0
num_lines = sum(1 for line in f)
newline = [0] * num_lines
print (num_lines)
for i in range(1, num_lines):
if i % 2 == 0:
newline[i] = f.readlines()[i]
print i, newline[i]
i = i + 1
f.close()
f = open('output.txt', 'w')
for j in range(0,num_lines):
if j % 2 == 0:
f.write(newline[j] + '\n')
j = j + 1
f.close()
Output:
17
Traceback (most recent call last):
File "./5", line 10, in <module>
a = f.readlines()[1]
IndexError: list index out of range
Upvotes: 0
Views: 6782
Reputation: 142106
You've also got the itertools.islice
approach available:
from itertools import islice
with open('input') as fin, open('output', 'w') as fout:
fout.writelines(islice(fin, None, None, 2))
This saves the modulus operation and puts the line writing to system level.
Upvotes: 3
Reputation: 133849
In your original script you read the file once to scan the number of lines, then you (try to) read the lines in memory, you needlessly create a list for the full size instead of just extending it with list.append
, you initialize the list with zeroes which does not make sense for a list containing strings, etc.
Thus, this script does what your original idea was, but better and simpler and faster:
with open('input.txt', 'r') as inf, open('output.txt', 'w') as outf:
for lineno, line in enumerate(inf, 1):
if lineno % 2 == 0:
outf.write(line)
Specifically
Upvotes: 3
Reputation: 121964
After
num_lines = sum(1 for line in f)
The file pointer in f
is at the end of the file. Therefore any subsequent call of f.readlines()
gives an empty list. The minimal fix is to use f.seek(0)
to return to the start of the file.
However, a better solution would be to read through the file only once, e.g. using enumerate
to get the line
and its index i
:
newline = []
for i, line in enumerate(f):
if i % 2 == 0:
newline.append(line)
Upvotes: 3