小编Aks*_*hay的帖子

我已经在python中编写了一个代码来打开并读取一个lnumber并希望找到所有数字的总和,但我不是getiingoutput

问题是读取文件,使用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
推荐指数
1
解决办法
791
查看次数

标签 统计

python ×1