I have a text file and I need to read from the seconds line to to 15th line including. I've tried some methods but no method worked for me... I'd be happy if anyone could help me ... thanks a lot!
Method 1: fileobject.readlines() A file object can be created in Python and then readlines() method can be invoked on this object to read lines into a stream. This method is preferred when a single line or a range of lines from a file needs to be accessed simultaneously.
Use open(file) to open file . Call file. readlines() with slicing syntax [1:] to skip the first line of the file.
Use itertools.islice
:
from itertools import islice
with open('filename') as fin:
for line in islice(fin, 1, 16):
print line
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