99问答网
所有问题
Python 从TXT文件中读取数字并放入list中,然后将数字相加输出结果
Python 从TXT文件中读取数字并放入list中,然后将数字相加输出结果
举报该问题
推荐答案 2018-03-13
#!/usr/bin/env python3.6
import re
fname = 'a.txt'
with open(fname) as f:
s = f.
read()
()
numbers = [int(i) for i in re.findall(r'\d+', s)]
print(f'numbers: {numbers}\nsum: {sum(numbers)}')
温馨提示:答案为网友推荐,仅供参考
当前网址:
http://99.wendadaohang.com/zd/zXeWOvztOBttXWWeOe.html
相似回答
大家正在搜
相关问题
python从txt文件中读取数字,并放入list中
用Python读取文件中的数字并加入list的问题
用python提取文档内全部数字并求和
python 从txt中读取数据到 list 中
python 读取txt文件特定字符串后面的数字,并写入到另...
python将txt文件中的字符和数字单独提取
python中读取一个.txt文件,并且放入数组。
如何用python提取txt中指定的列数据进行相加然后求平均...