I will read through and parse a file with text and numbers. I will extract all the numbers in the file and compute the sum of the numbers.
Data Files: http://python-data.dr-chuck.net/regex_sum_361580.txt
My code is:
import re
sum = 0
file = open('regex_sum_361580')
for line in file:
numbers = re.findall('[0-9]+', line)
if not numbers:
continue
else:
for number in numbers:
sum = int(number)
print (sum)
Also, is there an easier way to solve this problem?