问题是读取文件,使用re.findall()查找整数,查找"[0-9] +"的正则表达式,然后将提取的字符串转换为整数并汇总整数.
我的代码:sample.txt是我的文本文件
import re
hand = open('sample.txt')
for line in hand:
line = line.rstrip()
x = re.findall('[0-9]+',line)
print x
x = [int(i) for i in x]
add = sum(x)
print add
Run Code Online (Sandbox Code Playgroud)
OUTPUT:

python ×1