I'm trying to read a .txt file (data is ASCII textbook material stuff) with strings of numbers scattered throughout the file. I'm trying to extract those numbers to tag them to a list using regex and eventually add all the values as integers into a sum variable and print it. The problem is when I run this code:
import re
hand = open('regexTextData.txt')
numbers = list()
for line in hand:
if len(line) == 0: continue
extractedNumbers = re.findall('[0-9+]', line)
numbers = extractedNumbers + numbers
total = 0
for i in range(len(numbers)):
value = int(numbers[i])
total = total + value
print(total)
I run into an error:
Traceback (most recent call last):
File "sum_numbers_in_text_regex.py", line 13, in <module>
value = int(numbers[i])
ValueError: invalid literal for int() with base 10: '+'
What exactly went wrong here? I tried looking at other solutions but to no avail. If I missed a page that covered it I would like to know please.
Thanks ahead of time for reading