【问题标题】:Python ValueError: float: Argument: . is not number on line 12Python ValueError:浮点数:参数:。不是第 12 行的数字
【发布时间】:2017-10-07 02:20:33
【问题描述】:

好的,所以我是 python 新手,我目前正在为每个人学习 python 课程 (py4e)。

我们第 7.2 课的作业是:

7.2 编写一个提示输入文件名的程序,然后打开该文件并通读该文件,查找以下形式的行:

X-DSPAM-Confidence:    0.8475

计算这些行并从每行中提取浮点值并计算这些值的平均值并产生如下所示的输出。不要在解决方案中使用 sum() 函数或名为 sum 的变量。 测试时可以在http://www.py4e.com/code3/mbox-short.txt下载示例数据,下面输入mbox-short.txt作为文件名。

我想不通。我不断收到此错误

ValueError: float: Argument: . is not number on line 12

当我运行这段代码时(见截图):https://gyazo.com/a61768894299970692155c819509db54 第 12 行,即num = float(balue) + float(num) 继续运行。当我从 balue 移除浮子时,我得到另一个说

“TypeError:无法在第 12 行连接 'str' 和 'float' 对象”。

参数可以转换成浮点数还是只是一个字符串?这可能是问题所在,但我不知道这是否属实,即使是这样,我也不知道在那之后如何修复我的代码。

【问题讨论】:

  • 不,谢谢。这不是一个可以为您完成作业的网站。如果您的代码有特定问题,请参阅如何创建minimal reproducible example
  • 您会删除代码的屏幕截图并将其替换为问题中的文本格式的代码吗?作为图像的代码与剪贴板、搜索引擎和屏幕阅读器不兼容,因此不是提供工作的理想格式。这个问题可能在此基础上被搁置,但如果你能修复它,我们可以停止它关闭(或者我们可以在它关闭后重新打开它,如果涉及到那个)。
  • 展示您尝试过的内容并告诉我们您发现了什么(在本网站或其他地方)以及为什么它不能满足您的需求。当您提供研究时,您可以获得更好的答案。

标签: python python-3.x


【解决方案1】:

我会说你的方法还不错。但是,我不明白您对 for balue in linez 的意图,因为这是对 linez 中包含的字符的迭代。你宁愿想要浮动(linez)。我想出了一个类似这样的解决方案:

fname = raw_input("Enter file name: ")
print(fname)

count = 0
num = 0
with open(fname, "r") as ins:
    for line in ins:
        line = line.rstrip()
        if line.startswith("X-DSPAM-Confidence:"):
            num += float(line[20:])
            count += 1

print(num/count)

这只是为了让你走上正轨,我没有验证答案或脚本的正确性,因为这应该包含在你的作业中。

【讨论】:

    【解决方案2】:

    我意识到这已经得到解答 - 我正在做同样的课程,并且在那个确切的问题上我收到了同样的错误信息。这是由于该行被读取为非浮点数,我不得不将其读取为

    number =float((line[19:26]))
    

    顺便说一句,课程中的 Python 环境对字符串中的空格非常敏感——我刚刚得到了正确的代码,但它拒绝了它,因为我有“:”,而正确的答案是“:”——只花了半个冒号小时。

    只是为了它,这是我得到的答案,已被接受为正确的答案。希望你最终到达那里。

    # Use the file name mbox-short.txt as the file name
    
    count = 0
    average = 0
    
    filename = input("Enter file name: ")
    filehandle = open(filename)
    
    for line in filehandle:
        if not line.startswith("X-DSPAM-Confidence:") : continue
    
        line = line.rstrip()
        number =float((line[19:26]))
    
        count = count + 1
        average = average + number
    
    average = (average / count)
    average = float(average)
    print("Average spam confidence:",average)
    

    【讨论】:

      【解决方案3】:
      #Take input from user
      fname = input("Please inser file name:")
      
      
      #try and except
      try:
          fhand = open(fname)
      except:
          print("File can not be found", fname)
          quit()
      
          
      #count and total
      
      count= 0
      total = 0
      
      for line in fhand:
          line = line.rstrip()
          if not line.startswith("X-DSPAM-Confidence:"):
              continue
      
          else:
              line = line[20:]
              uline = line.upper()
              fline = float(uline)
      
              count = count+1
              total = total + fline
              avr = total / count
      
      print("Average spam confidence:" ,avr )
      

      【讨论】:

        【解决方案4】:

        您的问题的简短答案是修复字符串的切片以提取数字。

        如果您正在寻找冒号字符,“:”来查找浮点数, 请记住,您必须在切片中添加 +1 来切片字符串。如果你不这样做,你最终会得到----->:0.8475 这不是一个数字。 因此,在字符串的起始索引中添加 +1 来切片字符串,您将得到修复。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-11
          • 2018-04-23
          • 2015-03-25
          • 2021-12-23
          • 2017-08-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多