MacLovin - 5 months ago 25
Python Question

# Sum of strings extracted from text file using regex

I am just learning python and need some help for my class assignment.

I have a file with text and numbers in it. Some lines have from one to three numbers and others have no numbers at all.

I need to:

1. Extract numbers only from the file using regex

2. Find the sum of all the numbers

I used regex to extract out all the numbers. I am trying to get the total sum of all the numbers but I am just getting the sum of each line that had numbers. I have been battling with different ways to do this assignment and this is the closest I have gotten to getting it right.

I know I am missing some key parts but I am not sure what I am doing wrong.

Here is my code:

``````import re
text = open('text_numbers.txt')

for line in text:
line = line.strip()
y = re.findall('([0-9]+)',line)

if len(y) > 0:
print sum(map(int, y))
``````

The result I get is something like this
(each is a sum of a line):

14151

8107

16997

18305

3866

And it needs to be one sum like this (sum of all numbers):

134058

``````import re
import np
text = open('text_numbers.txt')
final = []
for line in text:
line = line.strip()
y = re.findall('([0-9]+)',line)

if len(y) > 0:
lineVal = sum(map(int, y))
final.append(lineVal)
print "line sum = {0}".format(lineVal)
print "Final sum = {0}".format(np.sum(final))
``````

Is that what you're looking for?