Extract Numbers from Text File

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to extract numbers greater than 5 from a text file and print them out.

👀 Preview

$ python FindDigits.py
876

🎯 Tasks

In this project, you will learn:

  • How to open a text file and read its contents
  • How to extract specific numbers from a string
  • How to concatenate the extracted numbers into a new string
  • How to print the extracted numbers

🏆 Achievements

After completing this project, you will be able to:

  • Manipulate strings and extract specific data from them
  • Write a Python script to automate a simple data processing task
  • Apply your knowledge of file handling and string operations in Python

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/FileHandlingGroup -.-> python/file_opening_closing("`Opening and Closing Files`") python/FileHandlingGroup -.-> python/file_reading_writing("`Reading and Writing Files`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/with_statement -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/variables_data_types -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/numeric_types -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/type_conversion -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/conditional_statements -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/for_loops -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/tuples -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/standard_libraries -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/file_opening_closing -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/file_reading_writing -.-> lab-302772{{"`Extract Numbers from Text File`"}} python/build_in_functions -.-> lab-302772{{"`Extract Numbers from Text File`"}} end

Open the String.txt File and Read the String

In this step, you will learn how to open the String.txt file and read the string.

  1. Open your text editor and create a new file called FindDigits.py in the /home/labex/project directory.
cd /home/labex/project
touch FindDigits.py
  1. In the FindDigits.py file, use the open function to open the String.txt file in the same directory and read the string.
## Open the String.txt file in the same directory and read the string
with open("String.txt", "r") as f:
    string = f.read()

The with statement is used to ensure that the file is properly closed after the reading is complete, even if an exception is raised.

Extract the Numbers Greater Than 5 From the String

In this step, you will learn how to extract the numbers greater than 5 from the string and concatenate them into a new string.

  1. Initialize an empty string to store the extracted numbers.
## Initialize an empty string to store the extracted numbers
numbers = ""
  1. Loop through each character in the string and check if it is a digit and greater than 5. If so, append it to the numbers string.
## Loop through each character in the string
for char in string:
    ## If the character is a digit and greater than 5
    if char.isdigit() and int(char) > 5:
        ## Append it to the numbers string
        numbers += char

The isdigit() method checks if the character is a digit, and the int(char) converts the character to an integer to check if it is greater than 5.

Print the Extracted Numbers

In this step, you will learn how to print the extracted numbers.

  1. Print the numbers string.
## Print out the numbers string
print(numbers)

The final FindDigits.py file should look like this:

## Open the String.txt file in the same directory and read the string
with open("String.txt", "r") as f:
    string = f.read()

## Initialize an empty string to store the extracted numbers
numbers = ""

## Loop through each character in the string
for char in string:
    ## If the character is a digit and greater than 5
    if char.isdigit() and int(char) > 5:
        ## Append it to the numbers string
        numbers += char

## Print out the numbers string
print(numbers)

You have now completed the project. Run the FindDigits.py file to see the output.

$ python FindDigits.py
876

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like