【问题标题】:Python programming error re: reading from filesPython编程错误回复:从文件中读取
【发布时间】:2015-03-19 02:28:23
【问题描述】:

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

“编写一个提示输入文件名的程序,然后打开该文件并通读该文件,查找以下形式的行: 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”

我错过了什么?

【问题讨论】:

  • 试着找出你的变量的值是什么。
  • 一方面你没有找到任何东西......如果不是 line.strip().startswith("X-DSPAM-Confidence: 0.8475")......这将找到开始的行with if not line.strip().startswith("X-DSPAM-Confidence: 0.8475"),但是除非它们都说 X-DSPAM-Confidence: 0.8475,否则您不会找到其他行。因此,您没有返回带有浮点数(总计/计数)的数字,因为您没有附加或添加任何数字
  • 当你得到正确的前缀时,你不会跳出 for 循环。你应该颠倒那个条件。

标签: python python-2.7


【解决方案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))

我刚刚针对样本数据进行了测试,效果很好

【讨论】:

  • 只要确保您输入的文件名正确或文件路径正确,就可以正常工作。如果要在文件名中输入类型,例如“mbox-short.txt”,请确保脚本和文件在同一目录中
  • 所以我试过了,但我得到了一个错误,指出我试图在最后一行除以零
  • 我使用您在链接中提供的示例数据在 sublime 文本编辑器上使用 python 2.7 运行它。
  • 这是终端平均垃圾邮件置信度的输出:0.750719。我只是以零更改重新运行该代码。直接从网站上复制并粘贴到编辑器中。
  • 好吧,我知道为什么自动评分器不接受它并给我那个错误,但它正在发生,我没有改变它......
【解决方案2】:
#try the code below, it is working.
fname = raw_input("Enter file name: ")
count=0
value = 0
sum=0
fh = open(fname)
for line in fh:
    if not line.startswith("X-DSPAM-Confidence:") : continue
    pos = line.find(':')
    num = float(line[pos+1:])
    sum=sum+num
    count = count+1    
print "Average spam confidence:", sum/count

【讨论】:

    【解决方案3】:

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

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

    如果简化,您也可以跳过

    inp = fh.read()
    

    就行了

    for line in fh:
    

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

    【讨论】:

    • 你是对的格式错误,我把 0.8475 去掉了,但我仍然得到同样的错误。
    【解决方案4】:
    # Use the file name mbox-short.txt as the file name
    fname = raw_input("Enter file name: ")
    fh = open(fname)
    count = 0
    total = 0
    for line in fh:
        if not line.startswith("X-DSPAM-Confidence:") :     continue
        count = count + 1
       # print count
        num = float(line[20:])
        total +=num
       # print total
        average = total/count
    print "Average spam confidence:", average
    

    【讨论】:

      【解决方案5】:

      您检查字段是否正确的方式过于具体。您需要查找没有值的字段标题(请参见下面的代码)。此外,您的计数和总计需要在循环内进行。这是一个更简单的解决方案,它利用了 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))
      

      【讨论】:

        【解决方案6】:
        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
        

        【讨论】:

        • 请解释一下你在做什么。不要只写固定的代码。
        【解决方案7】:
        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)
        

        【讨论】:

          【解决方案8】:
          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
          

          【讨论】:

          • 请不要重复回答
          【解决方案9】:
              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)
          

          【讨论】:

            【解决方案10】:
            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
            

            【讨论】:

              【解决方案11】:
              total = float(num)
              

              你忘了在这里求和 num 浮点数。 应该是的

              total = total+num 
              

              【讨论】:

                【解决方案12】:
                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)
                

                【讨论】:

                • 请不要只发布代码作为答案,还要解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常更有帮助,质量更好,并且更有可能吸引投票
                • 您好,欢迎来到 SO!虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。请阅读tourHow do I write a good answer?
                【解决方案13】:

                一切正常!!!

                使用文件名 mbox-short.txt 作为文件名

                fname = raw_input("Enter file name: ")
                
                if len(fname) == 0:
                    fname = 'mbox-short.txt'
                
                fh = open(fname)
                count = 0
                tot = 0
                ans = 0
                
                for line in fh:
                    if not line.startswith("X-DSPAM-Confidence:") : continue
                    count = count + 1
                    num = float(line[21:])
                    tot = num + tot
                
                ans = tot / count
                print("Average spam confidence:", ans)
                

                【讨论】:

                  【解决方案14】:
                  # 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
                  

                  【讨论】:

                  • 请不要重复回答
                  【解决方案15】:
                  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 )
                  

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-11-14
                    • 1970-01-01
                    • 2018-02-12
                    • 1970-01-01
                    相关资源
                    最近更新 更多