I'm working on the assignment of a Python course in Coursera that involves using regular expressions. It tells us to
read through and parse a file with text and numbers. Extract all the numbers in the file and compute the sum of the numbers.
The data we use is this
Here's my code:
import re
handle = open('regex_sum_330187.txt')
for line in handle :
line = line.rstrip()
numbers = re.findall('([0-9]+)',line)
print(sum(numbers))
It turns out that the sum is 0. So I'm a bit confused. If re.findall()
is supposed to return a list of zero or more substrings, what's wrong with my code?