Extract Numbers from a Text File using Python

In this tutorial, we are going to show you how to extract numbers from a text file using Python. We’ll be using the regular expression module, re, to perform this task.

Step 1: Create the txt file

Create the “codeigo.txt” with the following text:

Hi, it's your 24th bithday
10 little pigs
My zip code is 28376

Step 2: Import the necessary modules

The first step in extracting numbers from a text file using Python is to import the necessary modules. In our case, we’ll need to import re, which is the regular expression module. Here’s how to import the module:

Step 3: Open and read the text file

After importing the necessary module, we’ll need to open the text file and read its contents. Here’s how to achieve that:

Here, we open the “codeigo.txt” file in read mode and assign its contents to the “text” variable.

Step 4: Extract numbers using regular expressions

The next step is to extract the numbers from the text variable using regular expressions. We can use the re.findall() function to achieve this. Here’s how to do that:

In this step, we use the findall() function to search for all the occurrences of the regular expression “\d+” in the “text” variable. The “\d+” regular expression matches one or more digits.

The findall() function returns all the matches as a list of strings, which we assign to the “numbers” variable.

Step 5: Print the extracted numbers

Finally, we’ll print the extracted numbers to the screen using a loop. Here’s how to do that:

In this step, we loop through the “numbers” list and print each element to the screen.

Complete code

Here’s the complete code to extract all the numbers from a text file using Python:

Final result:

24
10
28376

Conclusion

Extracting numbers from a text file using Python is a straightforward task that can be achieved using the regular expression module. With the steps outlined above, you can extract all the numbers from a text file with just a few lines of code.