ValueError:could not convertstd string convertto float:求助

ValueError: cannot convert float to NaN to integer
3 messages
Open this post in threaded view
Report Content as Inappropriate
ValueError: cannot convert float to NaN to integer
On Ubuntu 9.04, python 2.6.2, numpy 1.2.1 this gives a ValueError:
x = np.array([1,2,3])
x[0] = np.nan
ValueError: cannot convert float to NaN to integer
But on Debian squeeze, python 2.5.4, numpy 1.2.1 the assignment works
(well, the float nan is convert to the int 0):
x[0] = np.nan
& &array([0,1,2])
The numpy version numbers are the same. Is this a python issue?
BTW, is there a way to determine the svn revision number of numpy when
installed from binary?
_______________________________________________
NumPy-Discussion mailing list
Open this post in threaded view
Report Content as Inappropriate
Re: ValueError: cannot convert float to NaN to integer
On Wed, Jul 22, 2009 at 13:35, Keith Goodman&& wrote:
& On Ubuntu 9.04, python 2.6.2, numpy 1.2.1 this gives a ValueError:
& x = np.array([1,2,3])
& x[0] = np.nan
& ValueError: cannot convert float to NaN to integer
& But on Debian squeeze, python 2.5.4, numpy 1.2.1 the assignment works
& (well, the float nan is convert to the int 0):
& x[0] = np.nan
array([0,1,2])
& The numpy version numbers are the same. Is this a python issue?
Probably. They rationalized a bunch of float behavior in Python 2.6,
and this may be a consequence of that. grep the Python sources for
that error message, or parts of it, to try to track down where it
comes from.
& BTW, is there a way to determine the svn revision number of numpy when
& installed from binary?
Not reliably, I don't think.
Robert Kern
&I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth.&
& -- Umberto Eco
_______________________________________________
NumPy-Discussion mailing list
Open this post in threaded view
Report Content as Inappropriate
Re: ValueError: cannot convert float to NaN to integer
On Wed, Jul 22, 2009 at 11:51 AM, Robert Kern&& wrote:
& On Wed, Jul 22, 2009 at 13:35, Keith Goodman&& wrote:
&& On Ubuntu 9.04, python 2.6.2, numpy 1.2.1 this gives a ValueError:
&& x = np.array([1,2,3])
&& x[0] = np.nan
&& ValueError: cannot convert float to NaN to integer
&& But on Debian squeeze, python 2.5.4, numpy 1.2.1 the assignment works
&& (well, the float nan is convert to the int 0):
&& x[0] = np.nan
array([0,1,2])
&& The numpy version numbers are the same. Is this a python issue?
& Probably. They rationalized a bunch of float behavior in Python 2.6,
& and this may be a consequence of that. grep the Python sources for
& that error message, or parts of it, to try to track down where it
& comes from.
The same error shows up when running the numpy unit tests with python
2.6 (but not with python 2.5):
======================================================================
ERROR: Tests the min/max functions with explicit outputs
----------------------------------------------------------------------
Traceback (most recent call last):
& File &/usr/lib/python2.6/dist-packages/numpy/ma/tests/test_core.py&,
line 699, in test_minmax_funcs_with_output
& & result = npfunc(xm,axis=0,out=nout)
& File &/usr/lib/python2.6/dist-packages/numpy/core/fromnumeric.py&,
line 1569, in amin
& & return amin(axis, out)
& File &/usr/lib/python2.6/dist-packages/numpy/ma/core.py&, line 3119, in min
& & np.putmask(out, newmask, np.nan)
ValueError: cannot convert float NaN to integer
_______________________________________________
NumPy-Discussion mailing list
Loading...python中无法将string转化为float是什么原因,该怎样解决呢?_百度知道Hi,I run redis-2.6-rc5 on a debian-64bit
32GB machine and get this errormessage after some parsing.Htop show me that the server used only 5.5GB in total (3.7GB redis) .Actually I have not maxmemory limit.# redis-cli get
maxmemory(nil)Thanks for any hint.Christian--You received this message because you are subscribed to the Google Groups &Redis DB& group.To view this discussion on the web visit To post to this group, send email to redis-.To unsubscribe from this group, send email to redis-db+.For more options, visit this group at
Search Discussions
Aaah sorry wrong command , there is a limit.redis-cli config get
maxmemory1) &maxmemory&2) &&Am Mittwoch, 18. Juli :27 UTC+2 schrieb Christian:Hi,I run redis-2.6-rc5 on a debian-64bit
32GB machine and get this errormessage after some parsing.Htop show me that the server used only 5.5GB in total (3.7GB redis) .Actually I have not maxmemory limit.# redis-cli get
maxmemory(nil)Thanks for any hint.Christian--You received this message because you are subscribed to the Google Groups &Redis DB& group.To view this discussion on the web visit To post to this group, send email to redis-.To unsubscribe from this group, send email to redis-db+.For more options, visit this group at
Related Discussions
viewthread |
categories
user style
1 user in discussion
site design / logo & 2016 GrokbasePython基础(10) - 异常 - CyouGunSama - 推酷
Python基础(10) - 异常 - CyouGunSama
异常:程序出现了错误而在正常控制流以外采取的行为
Python中常见的异常:
1. NameError:尝试访问一个未声明的变量
&&& something
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
NameError: name 'something' is not defined
2. SyntaxError:解释器语法错误,是唯一不在运行时发生的异常
File &&stdin&&, line 1
SyntaxError: invalid syntax
3. IndexError:超出范围的值索引序列
&&& lst = []
&&& lst[1]
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
IndexError: list index out of range
try-except
定义了进行异常监控的一段代码,并提供了处理异常的机制
&& try_suite #监控异常
except Exception[,reason]:
&&& except_suite #异常处理代码
f = open('somefile','r')
... except IOError,e:
print 'could not open file:',e
could not open file: [Errno 2] No such file or directory: 'somefile'
上例中,只捕获了IOError异常,任何其它异常不会被捕获。
可以检测多种异常:
&& try_suite #监控这里的异常
except Exception1[,reason1]:
&&& except_suite1 #异常处理代码
except Exception2[,reason2]:
&&& except_suite2 #异常处理代码
&&& def safe_float(obj):
retval = float(obj)
except ValueError:
retval = 'could not convert non-number to float'
except TypeError:
retval = 'object type cannot be converted to float'
return retval
&&& safe_float('xy.z')
'could not convert non-number to float'
&&& safe_float([1,2])
'object type cannot be converted to float'
一个except语句可以检测多种异常,多个异常要放在一个元组中:
&& try_suite #监控这里的异常
except (Exception1 [,Exception2 [, …ExceptionN]]) [,reason]:
&&& except_suite #异常处理代码
&&& def safe_float(obj):
retval = float(obj)
except (ValueError,TypeError):
retval = 'could not convert to float'
return retval
&&& safe_float('xy.z')
'could not convert to float'
&&& safe_float([1,2])
'could not convert to float'
一个except语句可以检测多种异常,多个异常要放在一个元组中:
&& try_suite #监控这里的异常
except Exception,e:
&&& except_suite #异常处理代码
Exception是大部分异常类的基类,因此上述代码支持捕获大多异常。KeyboardInterrupt(用户中断执行Ctrl+C)和SystemExit(python解释器请求退出)不是由于代码错误条件引起的异常,如下为异常类的树:
-BaseException
& |- KeyboardInterrupt
& |- SystemExit
& |- Exception
&&&& |-所有内建异常
try-except的作用是提供一个可以提示错误或处理错误的机制,而不是一个错误过滤器,下面这种捕获所有异常并忽略错误不是一种合理的编程方式:
except: Exception:
避免把大片代码装入try-except中然后使用pass忽略掉错误。
可以捕获特定的异常并忽略它们,或是捕获所有的异常并采取特定的动作。
v异常参数:
异常也可以有参数,标准内建异常提供至少一个参数,指示异常原因的一个字符串
异常参数自身会组成一个元组,并存储为异常类的实例。
对于大多数内建异常,也就是从StandardError派生的异常,这个元组中只包含一个指示错误原因的字符串。操作系统或其他环境类型的错误,例如:IOError,元组中会把操作系统的错误编号放到错误字符串的前面
float('xyz')
... except Exception,e:
ValueError('could not convert string to float: xyz',)
&&& type(e)
&type 'exceptions.ValueError'&
&&& isinstance(e, ValueError)
&&& isinstance(e, Exception)
vtry-finally
&&& try-suite
&&& finally-suite #无论如何都执行
finally子句是无论异常是否发生,是否捕获都会执行的一段代码
当在try范围中产生一个异常时,会立即跳转到finally语句段,当finally中的所有代码都执行完毕后,会继续向上一层引发异常。
如果finally中的代码引发了另一个异常或由于return、break、continue语法而终止,原来的异常将丢失而且无法重新引发。
try-except-else-finally
在try语句块中所有代码都执行成功后,将会执行else子句。
&&& try_suite
except Exception1:
&&& suite_for_exception1
except (Exception2,Exception3,Exception4):
&&& suite_for_exception2_and_3_and_4
except (Exception6, Exception7), Argument67:
&&& suite_for_Exception6_and_7_plus_argument
&&& suite_for_all_other_exceptions
&&& always_execute_suite
无论你选择哪一种语法,至少要有一个except子句
try语句块中异常发生点后的剩余语句将被忽略,不会被执行,解释器将搜索处理器,一旦找到,就开始执行处理器中的代码。如果没有找到合适的处理器,那么异常就向上移交给调用者去处理,如果上层调用者也没有找到合适的处理器,则该异常会继续被向上移交,直到找到合适的处理器,如果到达最顶层仍然没有找到对应处理器,那么就认为该异常未处理,解释器会显示出跟踪记录,然后退出。
之前看到的异常都是由解释器触发的。程序员也可以在遇到错误时主动触发异常:
raise [SomeException, [args [, traceback]]]
SomeException:可以是异常类或实例,如果是一个实例,则不能再带args参数
args: 异常参数
traceback: 跟踪记录对象
&&& class MyException(Exception):
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
def __str__(self):
return 'MyException occered, length:%s, atleast:%s'%(self.length,self.atleast)
raise MyException(2,3)
... except MyException,e:
MyException occered, length:2, atleast:3
sys模块中的exc_info()函数,是另一种获取异常信息的途径,它提供了一个三元祖信息,比我们单纯用异常参数获取的信息多
&&& exc_tuple
(&type 'exceptions.ValueError'&, ValueError('could not convert string to float: xy.z',), &traceback object at
0x01C0DF08&)
从sys.exc_info()得到的元组为:
exc_type: 异常类
exc_value:异常类的实例
exc_traceback: 跟踪记录对象
断言是一句等价于布尔真的判定,如果表达式为假,触发AssertionError异常
assert expression[, arguments]
&&& assert 1==1
&&& assert 1==0
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
AssertionError
&&& assert 1==0, 'one does not equal zero'
Traceback (most recent call last):
File &&stdin&&, line 1, in &module&
AssertionError: one does not equal zero
用try-except可以捕获AssertionError异常:
assert 1==0 , 'one does not equal zero'
... except AssertionError,e:
print '%s:%s'%(e.__class__.__name__,e)
AssertionError:one does not equal zero
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致

我要回帖

更多关于 sql convert string 的文章

 

随机推荐