Read specific line from file python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers begin with the 0th index. There are various ways to read specific lines from a text file in python, this article is aimed at discussing them. 

    File in use: test.txt

    Read specific line from file 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. It can be easily used to print lines from any random starting index to some ending index. It initially reads the entire content of the file and keep a copy of it in memory. The lines at the specified indices are then accessed. 

    Example:

    Python3

    file = open('test.txt')

    content = file.readlines()

    print("tenth line")

    print(content[9])

    print("first three lines")

    print(content[0:3])

    Output 

    tenth line
     

    This is line 10.

    first three lines
     

    This is line 1.This is line 2.This is line 3.

    Method 2: linecache package 

    The linecache package can be imported in Python and then be used to extract and access specific lines in Python. The package can be used to read multiple lines simultaneously. It makes use of cache storage to perform optimization internally. This package opens the file on its own and gets to the particular line. This package has getline() method which is used for the same. 

    Syntax: 

    getLine(txt-file, line_number)

    Example:

    Python3

    import linecache

    particular_line = linecache.getline('test.txt', 4)

    print(particular_line)

    Output :

    This is line 5.

    Method 3: enumerate()

    The enumerate() method is used to convert a string or a list object to a sequence of data indexed by numbers. It is then used in the listing of the data in combination with for loop. Lines at particular indexes can be accessed by specifying the index numbers required in an array. 

    Example:

    Python3

    file = open("test.txt")

    specified_lines = [0, 7, 11]

    for pos, l_num in enumerate(file):

        if pos in specified_lines:

            print(l_num)

    Output

    This is line 1.
    This is line 8.
    This is line 12.

    For the sake of completeness, here is one more option.

    Let's start with a definition from python docs:

    slice An object usually containing a portion of a sequence. A slice is created using the subscript notation, [] with colons between numbers when several are given, such as in variable_name[1:3:5]. The bracket (subscript) notation uses slice objects internally (or in older versions, __getslice__() and __setslice__()).

    Though the slice notation is not directly applicable to iterators in general, the itertools package contains a replacement function:

    from itertools import islice
    
    # print the 100th line
    with open('the_file') as lines:
        for line in islice(lines, 99, 100):
            print line
    
    # print each third line until 100
    with open('the_file') as lines:
        for line in islice(lines, 0, 100, 3):
            print line
    

    The additional advantage of the function is that it does not read the iterator until the end. So you can do more complex things:

    with open('the_file') as lines:
        # print the first 100 lines
        for line in islice(lines, 100):
            print line
    
        # then skip the next 5
        for line in islice(lines, 5):
            pass
    
        # print the rest
        for line in lines:
            print line
    

    And to answer the original question:

    # how to read lines #26 and #30
    In [365]: list(islice(xrange(1,100), 25, 30, 4))
    Out[365]: [26, 30]
    

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

    Use readlines() to Read the range of line from the File You can use an index number as a line number to extract a set of lines from it. This is the most straightforward way to read a specific line from a file in Python. We read the entire file using this way and then pick specific lines from it as per our requirement.

    How do I read a specific line in a csv file in Python?

    Using reader.
    Step 1: In order to read rows in Python, First, we need to load the CSV file in one object. So to load the csv file into an object use open() method..
    Step 2: Create a reader object by passing the above-created file object to the reader function..
    Step 3: Use for loop on reader object to get each row..

    How do you print a specific line in a text file in Python?

    How to print specific lines from a file in Python.
    file = open("sample.txt").
    lines_to_print = [0, 2].
    for index, line in enumerate(file):.
    if ( index in lines_to_print):.
    print(line).
    file. close().

    How extract specific data from text file in Python?

    How to extract specific portions of a text file using Python.
    Make sure you're using Python 3..
    Reading data from a text file..
    Using "with open".
    Reading text files line-by-line..
    Storing text data in a variable..
    Searching text for a substring..
    Incorporating regular expressions..
    Putting it all together..