【发布时间】:2020-12-04 12:16:58
【问题描述】:
我想从通过line.startswith() 找到的文件中打印一些数字,但我想对它们求和,它们是字符串而不是浮点数,所以我无法添加它们。如何将它们转换为浮点数。
这是我的代码:
file='mbox.txt'
handle=open(file)
count=0
for line in handle:
if line.startswith('X-DSPAM-Confidence:'):
count=count+1
numeri(line[20:])
num='%g'%float(numeri.strip(' \n '))
n=sum(num)
print('Le stringhe che iniziano con "X-DSPAM-Confidence:" sono' ,count)
print(n)
对不起,英语和编码错误,但我在这方面很新
【问题讨论】:
-
使用
num='%g'%float(numeri.strip(' \n '))将其转换回字符串。只需使用num = float(numeri.strip())。
标签: python python-3.x file printing