Python ValueError:无法将字符串转换为浮点:变量中的空字符串

Python ValueError:无法将字符串转换为浮点:变量中的空字符串,python,python-2.7,Python,Python 2.7,我有一个值错误,我如何修复它,当我的脚本崩溃变量get empty string时,我尝试为ping编写脚本,并从服务器获取响应时间,如果时间超过100,我打印错误,如果时间低于100,我打印良好,但某处我从ping输出中获得空字符串,我不知道如何解决这个问题,如果您知道如何改进我的代码,我很高兴看到你的评论,我只是在培训python。先谢谢你 import subprocess import time import os import re def main(): host_lis

我有一个值错误,我如何修复它,当我的脚本崩溃变量get empty string时,我尝试为ping编写脚本,并从服务器获取响应时间,如果时间超过100,我打印错误,如果时间低于100,我打印良好,但某处我从ping输出中获得空字符串,我不知道如何解决这个问题,如果您知道如何改进我的代码,我很高兴看到你的评论,我只是在培训python。先谢谢你

import subprocess
import time
import os
import re


def main():
    host_list = ['google.com', 'facebook.com', 'gmail.com', 'vk.com', '114.141.102.124', '192.168.255.129', '8.8.8.8']
    while True:
        for item in host_list:
            command = "ping " + item + " -c 1"
            ping = subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
            ping_out = ping.communicate()[0]
            pattern_ping_time = r'(time=\d{0,4}.\d{0,2})'
            pattern_connection_lost = r'(100% packet loss)'
            time_r = re.findall(pattern_ping_time, ping_out)
            conn_lost = re.findall(pattern_connection_lost, ping_out)
            time_r_s = ''.join(time_r)
            time_r_s_form = time_r_s.strip('time=')
            print(time_r_s_form)
            time_r_s_form_float = float(time_r_s_form)
            print(time_r_s_form_float)
            if time_r_s_form_float > 100.0:
                print(item + "  " + '\x1b[0;30;41m' + "bad" + '\x1b[0m')
            elif time_r_s_form_float < 100.0:
                print(item + "  " + '\x1b[6;30;42m' + "good" + '\x1b[0m')
            elif conn_lost:
                print(item + "  " + '\x1b[0;30;41m' + "connection lost" + '\x1b[0m')
            else:
                print("no data")
        time.sleep(1)
        os.system('clear')


if __name__=="__main__":
    main()
导入子流程
导入时间
导入操作系统
进口稀土
def main():
host_list=['google.com'、'facebook.com'、'gmail.com'、'vk.com'、'114.141.102.124'、'192.168.255.129'、'8.8.8']
尽管如此:
对于主机列表中的项目:
command=“ping”+项目+“-c1”
ping=subprocess.Popen(命令,stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
ping\u out=ping.communicate()[0]
模式(时间=\d{0,4}.\d{0,2})
模式\连接\丢失=r'(100%数据包丢失)'
时间=re.findall(模式锁定时间,锁定输出)
连接丢失=返回findall(模式连接丢失,ping out)
时间=''。加入(时间)
time\r\u s\u form=time\r\u.strip('time='))
打印(时间表格)
时间\r\u形式\u浮点=浮点(时间\r\u形式)
打印(时间、格式、浮动)
如果时间浮点数>100.0:
打印(项目+“”+'\x1b[0;30;41m'+“坏”+'\x1b[0m')
elif time\r\u s\u form\u float<100.0:
打印(项目+“”+'\x1b[6;30;42m'+“良好”+'\x1b[0m')
埃利夫·康努(elif conn_)输了:
打印(项+'+'\x1b[0;30;41m'+“连接丢失”+'\x1b[0m')
其他:
打印(“无数据”)
时间。睡眠(1)
操作系统(“清除”)
如果名称=“\uuuuu main\uuuuuuuu”:
main()
输出:

47.2
47.2
google.com  good
39.5
39.5
facebook.com  good
45.5
45.5
gmail.com  good
72.3
72.3
vk.com  good

Traceback (most recent call last):
  File "script.py", line 63, in <module>
    main()
  File "script.py", line 22, in main
    time_r_s_form_float = float(time_r_s_form)
ValueError: could not convert string to float: 
47.2
47.2
好的
39.5
39.5
很好
45.5
45.5
gmail.com很好
72.3
72.3
好的
回溯(最近一次呼叫最后一次):
文件“script.py”,第63行,在
main()
文件“script.py”,第22行,主
时间\r\u形式\u浮点=浮点(时间\r\u形式)
ValueError:无法将字符串转换为浮点:

尝试添加
print(repr(ping_out))
查看正则表达式在
114.141.102.124
中找不到任何内容的原因字节的数据。\n\n---114.141.102.124 ping统计信息---\n1个数据包传输,0个接收,100%数据包丢失,时间0ms\n\n'Good。现在确定如果发生这种情况,您希望您的程序执行什么操作。可能会显示错误消息?您可以发短信告诉我什么是“repr”吗?Google是您的朋友。