Read Specific Lines From a File in Python (2023)

This article lets you know how to read a specific lines from a file by line number in Python.

Table of contents

  • Steps To Read Specific Lines From A File
    • Example: Read specific lines from file by line number
  • linecache Module Read line from a file by line number
  • Use readlines() to Read the range of line from the File
  • Generator to Read Lines from a file by line number
  • for Loop in fileobject to Read Specific Lines in Python
  • Conclusion

Steps To Read Specific Lines From A File

Let’s assume the file to read is significantly large (in GB), and you don’t want to read the whole file in memory at once but only want to jump and read lines #5 and #120. To read specific lines from a text file, Please follow these steps:

  1. Open file in Read Mode

    To open a file pass file path and access mode r to the open() function. The access mode specifies the operation you wanted to perform on the file, such as reading or writing.
    For example, fp= open(r'File_Path', 'r') to read a file.

  2. Create a list to store line numbers

    Create a list with the number of each line in a text file to read.
    For example, line_numbers = [4, 7]. Here we are reading lines 4 and 7.

    (Video) Reading Files with Multiple Lines in Python

  3. Create a list to store lines

    After reading line 4 and 7 we will store result it in a list variable.

  4. Use for loop with enumerate() function to get a line and its number.

    The enumerate() function adds a counter to an iterable and returns it in enumerate object. Pass the file pointer returned by the open() function to the enumerate().
    We can use this enumerate object with a for loop to access the line number.
    Note: enumerate(file_pointer) doesn’t load the entire file in memory, so this is an efficient solution.

  5. Read file by line number

    Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.

Example: Read specific lines from file by line number

The following code showshow to read a text file by line number in Python. See the attached file used in the example and an image to show the file’s content for reference.

In this example, we are reading line number 4 and 7 and storing it in a list variable.

Read Specific Lines From a File in Python (1)
with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # lines to read line_numbers = [4, 7] # To store lines lines = [] for i, line in enumerate(fp): # read line 4 and 7 if i in line_numbers: lines.append(line.strip()) elif i > 7: # don't read after line 7 to save time breakprint(lines)

Output:

['Fifth line', 'Eighth Line']

linecache Module Read line from a file by line number

In this section, we’ll see how to read file by line number using a linecache module.

(Video) How to Read from a text .txt file in Python! Pulling in data and filtering and modifying the info!

Python’s linecache is another performance-optimized way to jump to a particular line in a text file. Assume if you have a large text file, then linecache is the correct choice.

The linecache module allows one to get any line from a Python source file, while attempting to optimize internally, using a cache, the common case where many lines are read from a single file. This is used by the traceback module to retrieve source lines for inclusion in the formatted traceback.

Python Documentation

Use the linecache.getline() method to read specific line from a file.

linecache.getline(filename, lineno, module_globals=None)
  • Get line lineno from a file named filename. This function will not return any error if the line is not present in a file instead, it will return an empty string.
  • Change the lineno to your desired line number, and you’re ready to go.

Example

import linecache# read fifth lineline = linecache.getline(r"E:\demos\files\read_demo.txt", 5)print(line)

Output:

Fifth line

Note: The linache reads the whole file in memory. So, if random access to line number is more important than performance, then use linache.

If you want to read more than one line number from a file using linecache use the below example.

import linecacheline_numbers = [2, 4]lines = []for i in line_numbers: x = linecache.getline(r"E:\demos\files\read_demo.txt", i).strip() lines.append(x)print(lines)

Use readlines() to Read the range of line from the File

If your file size is small and you are not concerned with performance, then the readlines() method is best suited.

(Video) Write a Python Program to Read a Random Line From a File

Reading a file in Python is fast if the file size is in a few MB.

The readlines() method reads all lines from a file and stores it in a list. 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.

Use readlines()[start:end] to read range of lines.

  • the start is the starting line number
  • the end is the last line number
  • To read from line number 3 to 5 use readlines()[2:5]
  • To read a single line use fp.readlines()[2]. this will read the third line.

Example: Read line from 3 to 5

with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line number 3 to 5 # index starts from 0 x = fp.readlines()[2:5] print(x)

Example: Read line 8

with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 8 x = fp.readlines()[7] print(x)

You can also use the readline() method to read a file line by line, stop when you’ve gotten to the lines you want. Using this technique, we don’t need to read the entire file.

Example 3:

(Video) Python - Read Each Line From a File

lines = [2, 5]result = []i = 0with open("read_demo.txt", "r+") as fp: # access each line while True: line = fp.readline() # check line number if i in lines: result.append(line.strip()) # line number greater than needed exit the loop # lines[-1] give last item from list if i > lines[-1]: break; i = i + 1print(result)

Generator to Read Lines from a file by line number

A fast and compact solution could be a generator expression to read a file using the line numbers.

If the number of lines to be returned from a file vast, you should use the generator.

This solution accepts file pointer and line numbers to be read returns a generator object to iterate using a loop to get each line.

# read by line numbersdef get_lines(fp, line_numbers): return (x for i, x in enumerate(fp) if i in line_numbers)with open(r"E:\demos\files\read_demo.txt", 'r') as fp: # read line 4 and 7 lines = get_lines(fp, [3, 6]) # print each line for line in lines: print(line.strip())

Output:

Fourth lineSeventh Line

for Loop in fileobject to Read Specific Lines in Python

If your file size is small and you are not concerned with performance, then use looping technique.

  • Use for loop to iterate each line from a file object
  • Use the if condition in each iteration of a loop to check the line number. If it matches, then save that line into a list.

Note: This will load entire file in memory.

Example:

lines = [2, 5]result = []i = 0with open("read_demo.txt", "r+") as fp: # access each line for line in fp: # check line number if i in lines: result.append(line.strip()) i = i + 1print(result)

Output:

(Video) Python: Read files line by line

['Third line', 'Sixth Line']

Conclusion

  • Use readlines() or readline() and loop to iterate lines from a file object if the file size is small.
  • Use linecache for a more clean solution. It is fast if you are reading repeatedly or reading different lines from multiple files.
  • Use a loop and enumerate() for large files because we don’t need to load the entire file in memory.

FAQs

How do I extract specific portions of a text file using Python? ›

How to extract specific portions of a text file using Python
  1. Make sure you're using Python 3.
  2. Reading data from a text file.
  3. Using "with open"
  4. Reading text files line-by-line.
  5. Storing text data in a variable.
  6. Searching text for a substring.
  7. Incorporating regular expressions.
  8. Putting it all together.
Jun 30, 2020

How do you read 10 lines from a 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 I read a specific line from a CSV file in Python? ›

Using reader
  1. 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.
  2. Step 2: Create a reader object by passing the above-created file object to the reader function.
  3. Step 3: Use for loop on reader object to get each row.
Dec 20, 2021

What does read () Splitlines () do? ›

Python String splitlines() method is used to split the lines at line boundaries. The function returns a list of lines in the string, including the line break(optional).

How do I view a specific line in a file? ›

How to Display Specific Lines of a File in Linux Command Line
  1. Display specific lines using head and tail commands. Print a single specific line. Print specific range of lines.
  2. Use SED to display specific lines.
  3. Use AWK to print specific lines from a file.
Jul 29, 2022

How do I extract multiple parts of a file? ›

You can select multiple WinZip files, right click, and drag them to a folder to unzip them all with one operation.
  1. From an open folder window, highlight the WinZip files you want to Extract.
  2. Right click in the highlighted area and drag to the destination folder.
  3. Release the right mouse button.
  4. Choose WinZip Extract to here.

How do I find a specific substring in Python? ›

The find() string method is built into Python's standard library. It takes a substring as input and finds its index - that is, the position of the substring inside the string you call the method on.

How do I extract part of a text string? ›

Depending on where you want to start extraction, use one of these formulas:
  1. LEFT function - to extract a substring from the left.
  2. RIGHT function - to extract text from the right.
  3. MID function - to extract a substring from the middle of a text string, starting at the point you specify.

How do you read the first 7 lines of a file in Python? ›

Using the readlines() function (returns a list with each line in the file represented as a list item. To limit the number of lines returned, use the hint argument.
...
Algorithm (Steps)
  1. Create a variable to store the path of the text file.
  2. Enter the N value static/dynamic for printing the first N lines of a file.
Aug 18, 2022

How do you get the first 10 lines of a file? ›

To look at the first few lines of a file, type head filename, where filename is the name of the file you want to look at, and then press <Enter>. By default, head shows you the first 10 lines of a file.

What is the command to see top 10 lines of a file? ›

The head command is used to display the first lines of a file. By default, the head command will print only the first 10 lines.

How do I read specific rows in pandas? ›

In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.

How do I extract a specific data from a CSV file? ›

Steps
  1. Make a list of columns that have to be extracted.
  2. Use read_csv() method to extract the csv file into data frame.
  3. Print the exracted data.
  4. Plot the data frame using plot() method.
  5. To display the figure, use show() method.
May 7, 2021

How do I extract specific rows in Excel using Python? ›

you can declare a list first and then write a loop which will traverse the rows which value you need. After every iteration you can append the value to the list. after completing the loop you will get your desired extracted values. Save this answer.

Does ReadLine read all lines? ›

readlines() is used to read all the lines at a single go and then return them as each line a string element in a list. This function can be used for small files, as it reads the whole file content to the memory, then split it into separate lines.

What is the difference between read () Splitlines () and Readlines ()? ›

read() returns a string, which you split on newlines, returning a list of strings. f. readlines() does the same as above, it reads the entire file and splits on newlines.

What does Splitlines () do in Python? ›

The splitlines() method splits a string into a list. The splitting is done at line breaks.

Which command is used to extract specific lines records from a file? ›

The cut command offers many ways to extract portions of each line from a text file. It's similar to awk in some ways, but it has its own advantages and quirks. One surprisingly easy command for grabbing a portion of every line in a text file on a Linux system is cut.

How do I echo a line from a file? ›

Using '>>' with 'echo' command appends a line to a file. Another way is to use 'echo,' pipe(|), and 'tee' commands to add content to a file.

How do you read multiple lines in Python? ›

readlines() to read multiple lines using Python? The read function reads the whole file at once. You can use the readlines function to read the file line by line.

How do I extract the contents of a file? ›

To unzip files

To unzip the entire folder, right-click to select Extract All, and then follow the instructions. To unzip a single file or folder, double-click the zipped folder to open it. Then, drag or copy the item from the zipped folder to a new location.

How do you extract files properly? ›

To unzip on Windows, double-click the file, select "Extract All," select a folder, and then click "Extract." On a Mac, simply double-click the file to unzip it.

How do I open a .001 and .002 file? ›

Launch WinRAR/7zip, click the “File” menu and click “Open Archive.” 3. Navigate to the directory where the split ZIP files are located, select the first file in the sequence (. 001) and click “Open.”

What substring () and substr () will do? ›

The difference between substring() and substr()

substr() 's start index will wrap to the end of the string if it is negative, while substring() will clamp it to 0 . Negative lengths in substr() are treated as zero, while substring() will swap the two indexes if end is less than start .

How do you filter specific strings in Python? ›

Filter a list of string using filter() method. filter() method accepts two parameters. The first parameter takes a function name or None and the second parameter takes the name of the list variable as values. filter() method stores those data from the list if it returns true, otherwise, it discards the data.

What is difference between slice () substring () and substr ()? ›

slice() extracts parts of a string and returns the extracted parts in a new string. substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. substring() extracts parts of a string and returns the extracted parts in a new string.

How do you pull cells that contain specific text? ›

Find cells that contain text
  1. Select the range of cells that you want to search. ...
  2. On the Home tab, in the Editing group, click Find & Select, and then click Find.
  3. In the Find what box, enter the text—or numbers—that you need to find.

How do I extract text features? ›

The process of converting text data into numbers is called Feature Extraction from the text. It is also called text vectorization.
...
4. What are the Techniques?
  1. One Hot Encoding.
  2. Bag of Word (BOW)
  3. n-grams.
  4. Tf-Idf.
  5. Custom features.
  6. Word2Vec(Word Embedding)
May 31, 2022

What function you will use to extract part of a string? ›

Use the MID and FIND functions

One example is using the MID function with the FIND function to extract a particular substring from a cell range.

What does read () do in Python? ›

read() method in Python is used to read at most n bytes from the file associated with the given file descriptor. If the end of the file has been reached while reading bytes from the given file descriptor, os. read() method will return an empty bytes object for all bytes left to be read.

Which of the commands displays the first few lines from a file? ›

Use the head command to write to standard output the first few lines of each of the specified files or of the standard input.

Which command would you use to view the last ten lines of a file? ›

Use the tail command to write the file specified by the File parameter to standard output beginning at a specified point. This displays the last 10 lines of the accounts file.

How do you read the number of lines in a text file? ›

To do this, follow the steps below.
  1. Edit the file you want to view line count.
  2. Go to the end of the file. If the file is a large file, you can immediately get to the end of the file by pressing Ctrl + End on your keyboard.
  3. Once at the end of the file, the Line: in the status bar displays the line number.
Aug 31, 2020

How do I display 10th line to 20th line in a text file? ›

Type the following head command to display first 10 lines of a file named “bar.txt”:
  1. head -10 bar.txt.
  2. head -20 bar.txt.
  3. sed -n 1,10p /etc/group.
  4. sed -n 1,20p /etc/group.
  5. awk 'FNR <= 10' /etc/passwd.
  6. awk 'FNR <= 20' /etc/passwd.
  7. perl -ne'1..10 and print' /etc/passwd.
  8. perl -ne'1..20 and print' /etc/passwd.
Dec 18, 2018

Which command is used to display the lines? ›

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out).

Which command is used to calculate lines in a file? ›

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files.

How do you select specific rows in Python? ›

You can use one of the following methods to select rows in a pandas DataFrame based on column values:
  1. Method 1: Select Rows where Column is Equal to Specific Value df. loc[df['col1'] == value]
  2. Method 2: Select Rows where Column Value is in List of Values. df. ...
  3. Method 3: Select Rows Based on Multiple Column Conditions df.
Sep 14, 2021

How do I filter specific rows from a DataFrame Python? ›

You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows.

How do you select rows with specific text in a DataFrame in Python? ›

We can use str. contains() method to select rows whose column values contain some value or regex pattern. We can also use str. startswith(), str.

How do I extract specific data from a spreadsheet? ›

Select a cell in the database. On the Excel Ribbon's Data tab, click the Advanced button. In the Advanced Filter dialog box, choose 'Copy to another location'. For the List range, select the column(s) from which you want to extract the unique values.

How to read specific rows from CSV in pandas? ›

Read CSV Files
  1. Load the CSV into a DataFrame: import pandas as pd. df = pd.read_csv('data.csv') ...
  2. Print the DataFrame without the to_string() method: import pandas as pd. ...
  3. Check the number of maximum returned rows: import pandas as pd. ...
  4. Increase the maximum number of rows to display the entire DataFrame: import pandas as pd.

How do I extract data from a specific column in Python? ›

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.

How do you extract a fractional part in Python? ›

Python math function | modf()

Returns : This method returns the fractional and integer parts of number in a two-item tuple. Both parts have the same sign as the number.

How do I extract a specific item from a list in Python? ›

6 Easy Ways to Extract Elements From Python Lists
  1. Method 1: Use Slicing.
  2. Method 2: Use List Index.
  3. Method 3: Use List Comprehension.
  4. Method 4: Use List Comprehension with condition.
  5. Method 5: Use enumerate()
  6. Method 6: Use NumPy array()

How do you extract features from text data? ›

Understanding Bag-of-Words Model
  1. Step 1: Collect the data. ...
  2. Step 2: Construct the vocabulary. ...
  3. Step 3: Create Document Vectors. ...
  4. Step 1: Computing f_{ij}; Frequency of term i in document j. ...
  5. Step 2: Computing Normalized Term Frequency. ...
  6. Step 3: Compute Inverse Document Frequency (IDF) score for each term.
Dec 11, 2020

How do I extract a partial text from a cell? ›

Here is how to do this:
  1. Select the cells where you have the text.
  2. Go to Data –> Data Tools –> Text to Columns.
  3. In the Text to Column Wizard Step 1, select Delimited and press Next.
  4. In Step 2, check the Other option and enter @ in the box right to it. ...
  5. In Step 3, General setting works fine in this case. ...
  6. Click on Finish.

What is an extract fraction? ›

Fractional extraction involves the use of two solvents and is the most powerful separation means in extraction. Fractional extraction is a process of extracting a liquid from a liquid, in which two solutes are separated, with one leaving in the extract and one in the raffinate.

How do you extract an element in Python? ›

Here are 3 approaches to extract dictionary values as a list in Python:
  1. (1) Using a list() function: my_list = list(my_dict.values())
  2. (2) Using a List Comprehension: my_list = [i for i in my_dict.values()]
  3. (3) Using For Loop: my_list = [] for i in my_dict.values(): my_list.append(i)
Feb 25, 2022

What is the formula for fraction extraction? ›

fraction extracted, E

The fraction of the total quantity of a substance extracted (usually by the solvent) under specified conditions, i.e. EA=QQ where QA is the mass of A extracted and QA is the total mass of A present at the start. Notes: E may be expressed as a percentage, %E.

How do I extract specific rows in pandas? ›

How to Select Rows from Pandas DataFrame
  1. Step 1: Gather your data. ...
  2. Step 2: Create a DataFrame. ...
  3. Step 3: Select Rows from Pandas DataFrame. ...
  4. Example 1: Select rows where the price is equal or greater than 10. ...
  5. Example 2: Select rows where the color is green AND the shape is rectangle.
May 29, 2021

How do I get specific data from a list? ›

Quick Article Summary to get a specific element from a list:
  1. Get elements by index. use the operator [] with the element's index. use the list's method pop(index) use slicing lst[start:stop:step] to get several elements at once. ...
  2. Get elements by condition. use the filter() function. use a list comprehension statement.

How do I extract a specific value from a DataFrame? ›

get_value() function is used to quickly retrieve the single value in the data frame at the passed column and index. The input to the function is the row label and the column label.

How do you analyze data from a text? ›

Word Counts
  1. Step 1 – Find the text you want to analyze. ...
  2. Step 2 – Scrub the data. ...
  3. Step 3 – Count the words. ...
  4. Step 1 – Get the Data into a Spreadsheet. ...
  5. Step 2 – Scrub the Responses. ...
  6. Step 3 – Assign Descriptors. ...
  7. Step 4 – Count the Fragments Assigned to Each Descriptor. ...
  8. Step 5 – Repeat Steps 3 and 4.
Feb 12, 2017

How do I extract features from a dataset in Python? ›

The sklearn. feature_extraction module can be used to extract features in a format supported by machine learning algorithms from datasets consisting of formats such as text and image.
...
The latter is a machine learning technique applied on these features.
  1. Loading features from dicts. ...
  2. Feature hashing. ...
  3. Text feature extraction.

How do I separate multiple lines of text in one cell? ›

Below are the steps that will split multiple lines in a single cell into separate cells:
  1. Select the entire dataset that you want to split.
  2. Go to the Data tab.
  3. In the Data Tools group, click on the Text to Columns option.
  4. In the Text to Columns dialog box, in Step 1 of 3, select Delimited and click 'Next'.

How do I pull information from one cell into multiple rows? ›

Split the content from one cell into two or more cells
  1. Select the cell or cells whose contents you want to split. ...
  2. On the Data tab, in the Data Tools group, click Text to Columns. ...
  3. Choose Delimited if it is not already selected, and then click Next.

Videos

1. How to Read a Record From a File With Python
(Max O'Didily)
2. Python - Read from multiple files & Regex search pattern in files
(Extreme Automation - Kamal Girdher)
3. Python Tutorial: File Objects - Reading and Writing to Files
(Corey Schafer)
4. Python Basics - 28 part 1 - Check if Specific Words Exist in A File
(HYPED247)
5. Python: Sort File Line by Line
(Joe James)
6. Reading Files | Python | Tutorial 28
(Mike Dane)
Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated: 12/04/2022

Views: 5731

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.