Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python read text file from second line to fifteenth [closed]

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!

like image 709
Den1al Avatar asked Aug 24 '13 19:08

Den1al


People also ask

How do you read a specific line of a text file in Python?

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.

How do you skip the first line of a readline in Python?

Use open(file) to open file . Call file. readlines() with slicing syntax [1:] to skip the first line of the file.


1 Answers

Use itertools.islice:

from itertools import islice
with open('filename') as fin:
    for line in islice(fin, 1, 16):
        print line
like image 88
Jon Clements Avatar answered Oct 07 '22 04:10

Jon Clements