我正在编写一段代码,其中我想从mbox.text文件中搜索术语“X置信度: 0.8475”。到目前为止,我可以搜索字符串并计数它在文件中出现的次数。现在的问题是,每次该字符串出现在文本文件中时,我都必须添加该字符串(此处- 0.8475 )的末尾数字。我需要帮助,因为我停留在那里,无法计算出现在字符串末尾的浮点数的总数。
我的文件内容如下:
X-Content-Type-Message-Body: text/plain; charset=UTF-8
Content-Type: text/plain; charset=UTF-8
X-DSPAM-Result: Innocent
X-DSPAM-Processed: Sat Jan 5 09:14:16 2008
X-DSPAM-Confidence: 0.8475
X-DSPAM-Probability: 0.0000我的代码:
text_file = raw_input ("please enter the path of the file that you want to open:")
open_file = open ( text_file )
print "Text file has been open "
count = 0
total = 0.00000
for line in open_file:
if 'X-DSPAM-Confidence:' in line:
total =+ float(line[20:])
count = count + 1
print total/count
print "The number of line with X-DSPAM-Confidence: is:", count我怎么能这么做?
发布于 2016-03-10 05:53:16
切片返回的是一个列表,而不是一个值,添加的原位操作符是+=而不是=+。也就是说,你应该使用split。
total = 0.00000
for line in open_file:
if 'X-DSPAM-Confidence:' in line:
total += float(line.split()[-1]) # change here.
count = count + 1
print total/count或者更好地使用sum和len。
with open('test.txt') as f:
data = [float(line.split()[-1]) for line in f if line.strip().startswith('X-DSPAM-Confidence:')]
print(sum(data)/len(data))Python3.4或更高版本的解决方案使用来自mean模块的statistics。
from statistics import mean
with open('test.txt') as f:
data = [float(line.split()[-1]) for line in f if line.strip().startswith('X-DSPAM-Confidence:')]
print(mean(data))发布于 2016-03-10 06:03:56
print语句,就像一个神奇的8-Ball,告诉了所有人。
>>> print repr(line[20:])
' 0.0000\n'你只需比float所能选择的更多。把它缩小一点
total += float(line[21:-1])https://stackoverflow.com/questions/35908562
复制相似问题