Python编程错误:从文件中读取

时间:2015-03-19 02:28:24

标签: python python-2.7

我参加了在线课程,我们被分配了以下任务:

"编写一个提示输入文件名的程序,然后打开该文件并读取文件,查找表单行: X-DSPAM-置信度:0.8475 对这些行进行计数并从每条线中提取浮点值,并计算这些值的平均值并生成输出,如下所示。 您可以在下面测试时在http://www.pythonlearn.com/code/mbox-short.txt下载示例数据,输入mbox-short.txt作为文件名。"

所需的输出为:"平均垃圾邮件可信度:0.750718518519"

以下是我写的代码:

fname = raw_input("Enter file name: ")
fh = open(fname)
inp = fh.read()
for line in inp:
    if not line.strip().startswith("X-DSPAM-Confidence: 0.8475") : continue
pos = line.find(':')
num = float(line[pos+1:]) 
total = float(num)
count = float(total + 1)
print 'Average spam confidence: ', float( total / count )

我得到的输出是:"平均垃圾邮件可信度:nan"

我错过了什么?

14 个答案:

答案 0 :(得分:1)

values = []
#fname = raw_input("Enter file name: ")
fname = "mbox-short.txt"
with open(fname, 'r') as fh:
    for line in fh.read().split('\n'): #creating a list of lines
        if line.startswith('X-DSPAM-Confidence:'):
            values.append(line.replace('X-DSPAM-Confidence: ', '')) # I don't know whats after the float value

values = [float(i) for i in values] # need to convert the string to floats
print 'Average spam confidence: %f' % float( sum(values) / len(values))

我刚刚根据它可以正常工作的样本数据测试了这个

答案 1 :(得分:1)

Service

答案 2 :(得分:0)

我对这个问题的猜测是,实际的0.8475实际上只是一个例子,你应该找到所有的X-DSPAM-Confidence:行并读取这些数字。

此外,您添加的代码的缩进具有for循环之外的所有计算,我希望这只是上传的格式错误,否则这也是一个问题。

如果简化你也可以跳过

inp = fh.read()

行,只是做

for line in fh:

另一件需要注意的是,总数将始终只是您阅读的最后一个数字。

答案 3 :(得分:0)

{{1}}

答案 4 :(得分:0)

您检查它是否是正确字段的方式太具体了。您需要查找没有值的字段标题(请参阅下面的代码)。你的计数和总计也需要在循环中发生。这是一个更简单的解决方案,它使用了python的内置函数。使用这样的列表需要更多的空间,但在我看来,代码更容易阅读。

这个怎么样? :D

with open(raw_input("Enter file name: ")) as f:
    values = [float(line.split(":")[1]) for line in f.readlines() if line.strip().startswith("X-DSPAM-Confidence")]
    print 'Average spam confidence: %f' % (sum(values)/len(values))

我的输出:

  

平均垃圾邮件可信度:0.750719

如果你需要更精确的浮动:Convert floating point number to certain precision, then copy to String

编辑:因为你是python的新手,可能有点过于pythonic:P这是相同的代码扩展了一点:

fname = raw_input("Enter file name: ")
values = []
with open(fname) as f:
    for line in f.readlines():
        if line.strip().startswith("X-DSPAM-Confidence"):
            values.append(float(line.split(":")[1]))

print 'Average spam confidence: %f' % (sum(values)/len(values))

答案 5 :(得分:0)

fname = raw_input("Enter file name: ")
fh = open(fname)
x_count = 0
total_count = 0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    line = line.strip()
    x_count = x_count + 1
    num = float(line[21:])
    total_count = num + total_count
aver = total_count / x_count

print "average spam confidence:", aver

答案 6 :(得分:0)

user_data = raw_input("Enter the file name: ")
lines_list = [line.strip("\n") for line in open(user_data, 'r')]


def find_spam_confidence(data):
    confidence_sum = 0
    confidence_count = 0
    for line in lines_list:
        if line.find("X-DSPAM-Confidence") == -1:
            pass
        else:
            confidence_index = line.find(" ") + 1
            confidence = float(line[confidence_index:])
            confidence_sum += confidence
            confidence_count += 1
    print "Average spam confidence:", str(confidence_sum / confidence_count)

find_spam_confidence(lines_list)

答案 7 :(得分:0)

fname = raw_input("Enter file name: ")
fh = open(fname)
c = 0
t = 0
for line in fh:
    if line.startswith("X-DSPAM-Confidence:") : 
        c = c + 1
        p = line.find(':')
        n = float(line[p+1:])
        t = t + n

print "Average spam confidence:", t/c

答案 8 :(得分:0)

    fname = input("Enter file name: ")
    fh = open(fname)
    count = 0
    add = 0
    for line in fh:
        if line.startswith("X-DSPAM-Confidence:"):
        count = count+1
        pos = float(line[20:])
        add = add+pos
    print("Average spam confidence:", sum/count)

答案 9 :(得分:0)

fname = input('Enter the file name : ') # file name is mbox-short.txt
try:
    fopen = open(fname,'r') # open the file to read through it
except:
    print('Wrong file name') #if user input wrong file name display 'Wrong file name'
    quit()
count = 0  # variable for number of 'X-DSPAM-Confidence:' lines
total = 0  # variable for the sum of the floating numbers

for line in fopen: # start the loop to go through file line by line
    if line.startswith('X-DSPAM-Confidence:'): # check whether a line starts with 'X-DSPAM-Confidence:'
        count = count + 1 # counting total no of lines starts with 'X-DSPAM-Confidence:'
        strip = line.strip() # remove whitespace between selected lines
        nline = strip.find(':') #find out where is ':' in selected line
        wstring = strip[nline+2:] # extract the string decimal value
        fstring = float(wstring) # convert decimal value to float
        total = total + fstring  # add the whole float values and put sum in to variable named 'total'
print('Average spam confidence:',total/count) # printout the average value

答案 10 :(得分:0)

total = float(num)

您忘记在这里对 num 浮点数求和。 应该是

total = total+num 

答案 11 :(得分:0)

fname = input("Enter file name: ")
fh = open(fname)
count=0
avg=0
cal=0
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") :
        continue
    else:
        count=count+1
        pos = line.find(':')
        num=float(line[pos+1:])
        cal=float(cal+num)
        #print cal,count
avg=float(cal/count)
print ("Average spam confidence:",avg)

答案 12 :(得分:-1)

# Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
fh = open(fname,'r')
count=0
avg=0.0
cal=0.00 
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") :        
        continue
    else:
        count=count+1
        pos = line.find(':')
        num=float(line[pos+1:])
        cal=cal+num
        #print cal,count
avg=float(cal/count)
print "Average spam confidence:",avg

答案 13 :(得分:-3)

fname = raw_input("Enter file name: ")
fh = open(fname)
inp = fh.read()
i = 0
total = 0
count = 0
for line in inp:
    if not line.strip().startswith("X-DSPAM-Confidence: 0.8475"):
        continue
    pos = line.find(':')
    num = float(line[pos+1:]) 
    total += num
    count += 1
print 'Average spam confidence: ', float( total / count )