Wednesday, July 27, 2022

[SOLVED] Unwanted exit after a while: Error can't convert str to float

Issue

I have written a little script that reads every hour from a website and search for a certain string. This string is a number with which i want to calculate.

If i run the script with "nohup python3 /path/to/script &" it works for a while. After few hours somethimes some days or even weaks the script stops working with the error output in "~/nohup" that the float() can't convert the string in line 41.

line 41: current_value = float(html_content[temperature_pos_begin:temperature_pos_end])

Whole script: http://pastebin.com/AEY1Kafa


Solution

Use an exception handler to see what is causing the error. Perhaps they use some sort of placeholder if the temperature isn't available.

try:
    current_value = float(html_content[temperature_pos_begin:temperature_pos_end])
except ValueError:
    print "Failed to convert %r to a float"%html_content[temperature_pos_begin:temperature_pos_end]
    current_value = None # or something that makes sense


Answered By - John La Rooy
Answer Checked By - Gilberto Lyons (WPSolving Admin)