我只是学习正则表达式,我需要在文本文件中读取并查找数字的每个实例并找到所有数字的总和。
import re
sum = 0
list_of_numbers = list()
working_file = open("sample.txt", 'r')
for line in working_file:
line = line.rstrip()
working_list = re.findall('[0-9]+', line)
if len(working_list) != 1:
continue
print(working_list)
for number in working_list:
num = int(number)
list_of_numbers.append(num)
for number in list_of_numbers:
sum += number
print(sum)
我放print(working_list)
以尝试调试它,看看是否所有数字都被正确找到了,我通过手动扫描文本文件看到有些数字被跳过了其他人不是。我很困惑为什么我认为我的正则表达式保证任何具有任意数字位数的字符串都会被添加到列表中。
答案 0 :(得分:5)
您只验证只有一个号码的行,因此if len(working_list) != 1: continue
会跳过包含两个数字的行,基本上会说&#34;如果没有一个号码,那么这一行上的数字然后跳过&#34;,你的意思可能是if len(working_list) < 1: continue
答案 1 :(得分:0)
我会这样做:
import re
digits_re = re.compile(r'(\d+(\.\d+)?)')
with open("sample.txt", 'r') as fh:
numbers = [float(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))
或者就像您正在使用整数
import re
digits_re = re.compile(r'(\d+)')
with open("sample.txt", 'r') as fh:
numbers = [int(match[0]) for match in digits_re.findall(fh.read())]
print(sum(numbers))
答案 2 :(得分:0)
h = open('file.txt')
nos = list()
for ln in h:
fi = re.findall('[0-9]+', ln)
for i in fi:
nos.append(int(i))
print('Sum:', sum(nos))