首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python中文本文件的切片和切分

python中文本文件的切片和切分
EN

Stack Overflow用户
提问于 2016-03-10 05:47:10
回答 2查看 869关注 0票数 0

我正在编写一段代码,其中我想从mbox.text文件中搜索术语“X置信度: 0.8475”。到目前为止,我可以搜索字符串并计数它在文件中出现的次数。现在的问题是,每次该字符串出现在文本文件中时,我都必须添加该字符串(此处- 0.8475 )的末尾数字。我需要帮助,因为我停留在那里,无法计算出现在字符串末尾的浮点数的总数。

我的文件内容如下:

代码语言:javascript
复制
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

我的代码:

代码语言:javascript
复制
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

我怎么能这么做?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-10 05:53:16

切片返回的是一个列表,而不是一个值,添加的原位操作符是+=而不是=+。也就是说,你应该使用split

代码语言:javascript
复制
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

或者更好地使用sumlen

代码语言:javascript
复制
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

代码语言:javascript
复制
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))
票数 0
EN

Stack Overflow用户

发布于 2016-03-10 06:03:56

print语句,就像一个神奇的8-Ball,告诉了所有人。

代码语言:javascript
复制
>>> print repr(line[20:])
' 0.0000\n'

你只需比float所能选择的更多。把它缩小一点

代码语言:javascript
复制
total += float(line[21:-1])
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35908562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档