简体   繁体   English

如何从 .txt 文件中提取浮点值以用于 python 中的计算?

[英]How do I extract floating point values from .txt file to use for calculation in python?

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence: 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个 27 行的文件,例如 X-DSPAM-Confidence: 0.xxxxx,我需要从它们中提取数值以用于计算。

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence : 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个像 X-DSPAM-Confidence : 0.xxxxx 这样的 27 行文件,我需要从每个文件中提取数值用于计算。

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence : 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个像 X-DSPAM-Confidence : 0.xxxxx 这样的 27 行文件,我需要从每个文件中提取数值用于计算。

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence : 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个像 X-DSPAM-Confidence : 0.xxxxx 这样的 27 行文件,我需要从每个文件中提取数值用于计算。

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence : 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个像 X-DSPAM-Confidence : 0.xxxxx 这样的 27 行文件,我需要从每个文件中提取数值用于计算。

count = 0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") :
        print(line)
        count = count + 1
print(count)

There is a file with 27 lines like X-DSPAM-Confidence : 0.xxxxx, I need to extract the numerical value from each of them to be used for calculations.有一个像 X-DSPAM-Confidence : 0.xxxxx 这样的 27 行文件,我需要从每个文件中提取数值用于计算。

fname = input("Enter file name:")
fh = open(fname)
count = 0
s=0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:"):
        continue
    count = count+1
    pos = line.find('0')
    floatingP = float(line[pos:])
    s += floatingP
print(s/count)

use count and sum then sum/count enter image description here使用 count 和 sum 然后 sum/count在此处输入图像描述

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

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