Python:ValueError:float()的无效文字:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33832275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:04:12  来源:igfitidea点击:

Python: ValueError: invalid literal for float():

pythonpython-2.7numpy

提问by elly

I'm really new with python. So maybe my question is really basic...For my work I'm checking different parameters in a period of time. For the Beginning with Python I wanted to plot a simple List with daily measured Temperature-values for a month. In the List I've three splits like these following structure:

我对python真的很陌生。所以也许我的问题真的很基本......对于我的工作,我正在检查一段时间内的不同参数。对于 Python 的开始,我想绘制一个简单的列表,其中包含一个月内每日测量的温度值。在列表中,我有如下结构的三个拆分:

Day -TAB- Temperature -TAB- Nr

天 -TAB- 温度 -TAB- Nr

My Code:

我的代码:

import pylab as pl
import numpy as np

filename = "u_netCDF_write"
file = open(filename)
NoOfValues = 31                                                      

counter=0
data = []              
for line in file:                                                     
    if counter <= NoOfValues:                                          
         data.append(line.strip('\n').strip('\t').split(' '))            
         if len(data[-1]) == 4:                                         
            data[-1].pop(3)                                             
    counter+=1                                                          
x = np.linspace(0,30,31)                                                
data = np.transpose(data)                                             

for i in range(len(data[2])):                                           
   data[2][i] = float(data[2][i])-273.15

When I try to plot a Temperature-per-Day-Plot I get the Error-Message:

当我尝试绘制每日温度图时,我收到错误消息:

Traceback (most recent call last):
  File ".../.../unetCDFplot.py", line 43, in <module>
    data[2][i] = float(data[2][i])-273.15   
ValueError: invalid literal for float(): 03.07.2014

It looks like that the Code didn't transpose the Data. Why is that so? Can anybody help me? Thanks!

看起来代码没有转置数据。为什么呢?有谁能够帮助我?谢谢!

采纳答案by elly

I solved my problem! So for anybody who has the same problem, here is what I did: I used

我解决了我的问题!所以对于任何有同样问题的人,这就是我所做的:我用过

print(repr(data))

(thanks to Stephany Dionysio) to check every step in my code and understood that the problem was not the transpose-function, but the empty spaces in every line. After trying different methods to delete the empty spaces I saw that I couldn't delete an array in an array caused by 'data.append'. To get the values I needed I used pop() in the append method:

(感谢 Stephany Dionysio)检查我代码中的每一步,并了解到问题不在于转置函数,而是每一行中的空格。在尝试了不同的方法来删除空格后,我发现我无法删除由“data.append”引起的数组中的数组。为了获得我需要的值,我在 append 方法中使用了 pop():

data.append(line.strip('\n').strip('\t').split(' ').pop(7))  

Now my code works fine. Thank you for your good advices, they put me to the right way! :)

现在我的代码工作正常。谢谢你的好建议,他们让我走上了正确的道路!:)

回答by SZD

I don't know the content of your "u_netCDF_write" file so it is reasonably dificult to debug it. But as the other post shows, it cloud be a non-printing character that's present in the value.

我不知道您的“u_netCDF_write”文件的内容,因此调试它相当困难。但正如另一篇文章所显示的那样,它是一个存在于值中的非打印字符。

See if this helps python ValueError: invalid literal for float()

看看这是否有帮助 python ValueError: invalid literal for float()

回答by LegendaryDude

03.07.2014 cannot be a float. It looks like you're using the wrong data from your data list.

03.07.2014 不能是浮点数。看起来您使用了数据列表中的错误数据。