您可以使用python的
re
要对文件中的所有数字求和的模块:
In [1]: import re
In [2]: text = open('input_file.txt').read() # Read from file
In [3]: text
Out[3]: '12 34 55 22 (tab)\n3\n5\n6\n7 13\n'
In [4]: list_numbers = re.findall('\d+', text) # Extract all numbers using the regex '\d+', which matches one, or more consecutive digits
In [5]: list_numbers
Out[5]: ['12', '34', '55', '22', '3', '5', '6', '7', '13']
In [6]: sum([int(number) for number in list_numbers]) # Find the sum of all the numbers
Out[6]: 157
使用refidle的正则表达式匹配-
refiddle demo