Python Program to Extract Numbers from a Text File and Add Them

Given a text file and the task is to extract all the numbers present in a given text file and add/sum all those numbers that are present in the text file.

Program to Extract Numbers from a Text File and Add Them in Python

Approach:

  • Open some random text file in write mode and store it in a variable.
  • Write some content into the above opened text file using the write function.
  • Close the above opened text file using the close() function.
  • Take a variable and initialize its value with 0 (which stores the sum of all the digits in a given text file).
  • Open some random text file in read-only mode using the open() function.
  • Iterate in each line of the given text file using the for loop
  • Loop in each character of the corresponding line using another nested for loop
  • Check if each character in the corresponding line is a digit/number or not using the isdigit() function and if conditional statement.
  • If it is true, then add that number to the above intialized variable(which stores the sum of all the digits in a given text file).
  • Print the sum of all the digits present in a given text file.
  • The Exit of the Program.

Below is the Implementation:

# Open some random text file in write mode and store it in a variable
gvn_txtfile = open('demo_textfile.txt', 'w+')
# Write some content into the above opened text file using the write function
gvn_txtfile.write('hello 1 python 4362programs. welcome 6123 all')
# Close the above opened text file using the close() function.
gvn_txtfile.close()

# Take a variable and initialize its value with 0 
# which stores the sum of all the digits in a given text file.
digits_sum = 0
# Open some random text file in read-only mode using the open() function.
with open('demo_textfile.txt', 'r') as txtfile:
    # Iterate in each line of the given text file using the for loop
    for f_line in txtfile:
        # Loop in each character of the corresponding line using another nested for loop
        for character in f_line:
            # Check if each character in corresponding line is digit/number or not using the
            # isdigit() function and if conditional statement. 
            if character.isdigit():
                # If it is true, then add that number to the above intialized variable.
                # (which stores the sum of all the digits in a given text file).
                digits_sum = digits_sum + int(character)

# Print the sum of all the digits present a given text file.  
print("The sum of all the digits present a given text file = ", digits_sum)

File Input:

hello 1 python 4362programs. welcome 6123 all

Output:

The sum of all the digits present a given text file = 28

Note:

You can also take any text file and provide the path of the text file to the open() function to open the given text file and to calculate the sum of digits of the text file as shown below.

# Take a variable and initialize its value with 0 
# which stores the sum of all the digits in a given text file.
digits_sum = 0
# Open some random text file in read-only mode using the open() function.
with open('sampleTextFile.txt', 'r') as txtfile:
    # Iterate in each line of the given text file using the for loop
    for f_line in txtfile:
        # Loop in each character of the corresponding line using another nested for loop
        for character in f_line:
            # Check if each character in corresponding line is digit/number or not using the
            # isdigit() function and if conditional statement. 
            if character.isdigit():
                # If it is true, then add that number to the above intialized variable.
                # (which stores the sum of all the digits in a given text file).
                digits_sum = digits_sum + int(character)

# Print the sum of all the digits present a given text file.  
print("The sum of all the digits present a given text file = ", digits_sum)

sampleTextFile.txt

hello 1 python 4362programs. welcome 6123 all

Output:

The sum of all the digits present a given text file = 28