Python. Even-numbered lines in text file
Question:
So, i need a code to read onle even-numbered lines from a txt file.
Here is an example of a task.
Input:
Bravely bold Sir Robin rode forth from Camelot
Yes, brave Sir Robin turned about
He was not afraid to die, O brave Sir Robin
And gallantly he chickened out
He was not at all afraid to be killed in nasty ways
Bravely talking to his feet
Brave, brave, brave, brave Sir Robin
He beat a very brave retreat
Output:
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat
Answers:
Use itertools.islice:
import itertools
import sys
with open('input.txt') as f:
sys.stdout.writelines(itertools.islice(f, 1, None, 2))
something like this:
with open('in.txt','r') as f:
file = f.readlines()
for i in range(1,8,2):
print file[i]
it will print:
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat
So, just output the even lines.
i = 1
f = open('file')
for line in f.readlines():
if i % 2 == 0 :
print line
i += 1
This is how I would do it, presuming Python 3.x:
with open("input.txt") as f:
result = list(f)[1::2]
The first line opens the file, the with statement puts it in a context.
In this context (no pun intended) this means the file will automatically be closed.
The next line gets a list of the file, containing all the lines in it.
The list is then sliced, starting at position 1 and jumping 2 elements, or lines, each time.
This is then assigned to the variable result.
It may be a simple way.
with open(input(), "r") as f: # rosalind_ini5.txt
print("".join(f.readlines()[1::2]))
So, i need a code to read onle even-numbered lines from a txt file.
Here is an example of a task.
Input:
Bravely bold Sir Robin rode forth from Camelot
Yes, brave Sir Robin turned about
He was not afraid to die, O brave Sir Robin
And gallantly he chickened out
He was not at all afraid to be killed in nasty ways
Bravely talking to his feet
Brave, brave, brave, brave Sir Robin
He beat a very brave retreat
Output:
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat
Use itertools.islice:
import itertools
import sys
with open('input.txt') as f:
sys.stdout.writelines(itertools.islice(f, 1, None, 2))
something like this:
with open('in.txt','r') as f:
file = f.readlines()
for i in range(1,8,2):
print file[i]
it will print:
Yes, brave Sir Robin turned about
And gallantly he chickened out
Bravely talking to his feet
He beat a very brave retreat
So, just output the even lines.
i = 1
f = open('file')
for line in f.readlines():
if i % 2 == 0 :
print line
i += 1
This is how I would do it, presuming Python 3.x:
with open("input.txt") as f:
result = list(f)[1::2]
The first line opens the file, the with statement puts it in a context.
In this context (no pun intended) this means the file will automatically be closed.
The next line gets a list of the file, containing all the lines in it.
The list is then sliced, starting at position 1 and jumping 2 elements, or lines, each time.
This is then assigned to the variable result.
It may be a simple way.
with open(input(), "r") as f: # rosalind_ini5.txt
print("".join(f.readlines()[1::2]))