为什么这两种方法在提取和求和文本中的数字时都会出错?
Why both of the method go wrong in extracting and sum the numbers in a text?
目标是对文本中的所有数字求和,使用 re.findall()
。
我尝试了两种方法,两种方法都运行 ok,而其中none给出了真实的结果sum.And他们的结果似乎小于正确的总数number.The sample.txt 由几行文字和数字组成,例如“3036 many reasons, range from making your live to solving 7209”.
你能帮我找出问题所在吗?正确的代码应该是什么?
解决方案 1:
import re
hand = open ('sample.txt')
numlist = list ()
for line in hand :
line = line.rstrip()
stuff = re.findall ('[0-9]+',line)
if len(stuff) != 1 :continue
num=int(stuff[0])
numlist.append(num)
b=sum(numlist)
print (b)
解决方案 2:
import re
hand = open ('sample.txt')
s=0
for line in hand :
line = line.rstrip()
stuff = re.findall ('[0-9]+',line)
if len(stuff) != 1 :continue
s +=int(stuff[0])
print (s)
只需对整个文件应用 re.findall
..
hand = open ('sample.txt')
print sum([int(i) for i in re.findall(r'\d+', hand.read())])
hand.close()
目标是对文本中的所有数字求和,使用 re.findall()
。
我尝试了两种方法,两种方法都运行 ok,而其中none给出了真实的结果sum.And他们的结果似乎小于正确的总数number.The sample.txt 由几行文字和数字组成,例如“3036 many reasons, range from making your live to solving 7209”.
你能帮我找出问题所在吗?正确的代码应该是什么?
解决方案 1:
import re
hand = open ('sample.txt')
numlist = list ()
for line in hand :
line = line.rstrip()
stuff = re.findall ('[0-9]+',line)
if len(stuff) != 1 :continue
num=int(stuff[0])
numlist.append(num)
b=sum(numlist)
print (b)
解决方案 2:
import re
hand = open ('sample.txt')
s=0
for line in hand :
line = line.rstrip()
stuff = re.findall ('[0-9]+',line)
if len(stuff) != 1 :continue
s +=int(stuff[0])
print (s)
只需对整个文件应用 re.findall
..
hand = open ('sample.txt')
print sum([int(i) for i in re.findall(r'\d+', hand.read())])
hand.close()