简体   繁体   English

从 txt 文件的多行中提取某些值

[英]Extract certain values from multiple lines from a txt file

Similar questions have been asked and answered before, but I'd like to know why my code doesn't yield the correct output.之前已经提出并回答了类似的问题,但我想知道为什么我的代码没有产生正确的 output。

There are a couple of lines in my txt file that look like this: X-DSPAM-Confidence: 0.xxxx我的 txt 文件中有几行如下所示: X-DSPAM-Confidence: 0.xxxx

The 0.xxxx value varies. 0.xxxx 值会有所不同。 I need to slice that part out of each "X-DSPAM-Confidence:" line and calculate an average.我需要从每个“X-DSPAM-Confidence:”行中切出该部分并计算平均值。

The txt file can be downloaded here: http://www.py4e.com/code3/mbox-short.txt txt文件可以在这里下载: http://www.py4e.com/code3/mbox-short.txt

My code is as follows:我的代码如下:

fname = input("Enter file name: ")
fh = open(fname)
count = 0
current = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue   # Please do not change this line and develop the program based on it
    count = count + 1  # I think this would count how many lines that starts with X-DSPAM-Confidence:
    pos = line.find(':')    # This should find me the position for ":"
    number = line[pos+5:]   # I think this should slice the number out
    final = float(number) + current    # Then I float the number and add to the current running number
print("Average spam confidence: ", final/count)    # Finally, when the loop finishes with the file, print the average

Using the code above, I got the average 33.5925925926, but the correct answer should be 0.750718518519.使用上面的代码,我得到了平均 33.5925925926,但正确答案应该是 0.750718518519。

Can someone please enlighten me?有人可以启发我吗?

As you requested, with minimal but essential changes.根据您的要求,进行最小但基本的更改。

    fname = input("Enter file name: ")
    with open(fname, "r") as current_file: 
        # i can not stress enough the importance of the with method over 
        #open()/close()
            content = current_file.readlines()

    count = 0
    current = 0
    for line in content:
        if not line.startswith("X-DSPAM-Confidence:"): continue
        count = count + 1  
        pos = line.find(':')    
        number = line[pos+2:]   # small error, it was not 5 but 2 instead
        current = float(number) + current
    print("Average spam confidence: ", current/count)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM