开发者

to overcome type error?

开发者 https://www.devze.com 2023-01-03 06:36 出处:网络
i have a file of this开发者_如何转开发 format: 3.334 1 2.345 1 1.453 1 3.343 1 and so on but in middle at times in file there are few number which are not in float format and i receive type msg whe

i have a file of this开发者_如何转开发 format:

3.334 1
2.345 1
1.453 1
3.343 1
and so on

but in middle at times in file there are few number which are not in float format and i receive type msg when i run them performation some operation..

I want to give a condition that:

if(not in float format):
  continue
else:
  perform operation

please tell me how to put the condtion


You can use an exception handler:

try:
    f = float(thing)
except ValueError:
    # This is not a float
    f = 0.0

ValueError is thrown by invalid conversions. You should have seen it in your application traceback when an invalid float value conversion was tried.

0

精彩评论

暂无评论...
验证码 换一张
取 消