bertrand2310
Posts: 3
Joined: Sun Feb 11, 2018 2:19 pm

Error string to float: !OVF

Mon Feb 12, 2018 3:55 pm

Hello,

I have a problem, when I launch a script in python, sometimes I receive this message:

Traceback (most recent call last):
File "test-Regu-V1.py", line 186, in <module>
regu_difference = regu_value - round(float(spectro_tabWatt))
ValueError: could not convert string to float: !OVF

Can you help me ?

Kevin

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Error string to float: !OVF

Mon Feb 12, 2018 4:08 pm

use a "try: except:" block to catch the exception and print out offending the value of spectro_tabWatt.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

MaxK1
Posts: 1043
Joined: Sun Aug 26, 2012 11:34 pm

Re: Error string to float: !OVF

Mon Feb 12, 2018 6:08 pm

We once had a large-ish energy management system that would happily take any random garbage string and convert it floating point, store it and keep on crunching (maybe). When we reported the error, the head office said (in essence) "Don't enter invalid data"... Then we found about 20000 other "problems" with the system...
You are in a maze of twisty little passages, all alike.
When General Failure and Major Disaster get together, Private Parts usually suffers.

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Error string to float: !OVF

Mon Feb 12, 2018 11:23 pm

Python has already told you what it can't convert right there in the exception, spectro_tabWatt = '!OVF', you need to validate your string. Easiest way to check if it can be converted into a float as PeterO said, using exceptions to catch when it can't.

Code: Select all

def is_float(v):
    try:
        float(v)
        return True
    except ValueError:
        return False

if is_float(spectro_tabWatt):
    # it is a float, do your calculations
    regu_difference = regu_value - round(float(spectro_tabWatt))
else:
    # do whatever to handle receiving invalid data
    print('Cannot convert contents of spectro_tabWatt into a floating point number: {}'.format(spectro_tabWatt))
She who travels light — forgot something.

Return to “Beginners”