Search code examples
pythonfor-loopsumtext-files

summing integers in a file in python


I have a file containing a random number of integers, the integers can be seperated by space, tab och by linebreak. i.e textfile.txt contains

12 34 55     22 (tab)
3
5
6
7 13

I know how to sum from a file containing only integers seperated by linebreak,

f=open('txtfile.txt')
sum = 0
for i in f:
        sum += int(i)

and summing from a line (seperated by either space or tab)

linesum = 0
aa=f.readline()
bb=aa.split()

for el in bb:
    nr = int(el)
    linesum += nr

running this on the first line in textfile.txt returns 123.

What im having trouble with is combining both of these to sum integers seperated by BOTH line breaks and blankspaces and tabs.

What I want the program to do is use the linesum program on lines containing more than 1 integer, else I want to use the linebreak sum program. However Im having problems generalizing the two for-loops into a program that checks which of the two is to be used. Any directions are greatly appreciated.


Solution

  • You can use python's re module to sum over all the numbers present in the file:

    In [1]: import re
    
    In [2]: text = open('input_file.txt').read() # Read from file
    
    In [3]: text
    Out[3]: '12 34 55     22 (tab)\n3\n5\n6\n7 13\n'
    
    
    In [4]: list_numbers = re.findall('\d+', text) # Extract all numbers using the regex '\d+', which matches one, or more consecutive digits
    
    In [5]: list_numbers
    Out[5]: ['12', '34', '55', '22', '3', '5', '6', '7', '13']
    
    In [6]: sum([int(number) for number in list_numbers]) # Find the sum of all the numbers
    Out[6]: 157