Python 如何跳过索引

Python 如何跳过索引,python,Python,运行上述程序时,它会停止运行,并返回以下错误: 回溯(最近一次调用):文件“/home/pi/v1.py”,第123行, 在里面 x3=结构包('>HH',abs(ReadValue[3]),abs(ReadValue[2]);索引器错误:元组索引超出范围 我希望我的程序即使返回错误也能持续运行。我想跳过错误并连续运行程序。我该怎么做 为了在发生此类错误时继续,只需将希望忽略异常的部分放在适当的try:。。。除了… #main program while True: R

运行上述程序时,它会停止运行,并返回以下错误:

回溯(最近一次调用):文件“/home/pi/v1.py”,第123行, 在里面 x3=结构包('>HH',abs(ReadValue[3]),abs(ReadValue[2]);索引器错误:元组索引超出范围


我希望我的程序即使返回错误也能持续运行。我想跳过错误并连续运行程序。我该怎么做

为了在发生此类错误时继续,只需将希望忽略异常的部分放在适当的
try:。。。除了…

#main program
while True:    

        ReadValue = Func03Modbus(1,70,40);#slave,start,number of registers
        x3 = struct.pack('>HH',abs(ReadValue[3]),abs(ReadValue[2])) 
        pressure = struct.unpack('>f', x3)
        print pressure[0]   
        c3 = struct.pack('>HH',abs(ReadValue[5]),abs(ReadValue[4])) 
        purity = struct.unpack('>f', c3)
        print purity[0] 
        hrs = int(ReadValue[30])
        mins= int(ReadValue[31])
        timein =float(str(ReadValue[30])+"."+str(ReadValue[31]))    
        print timein 
        r=requests.get("http://api.thingspeak.com/update api_key=5RMT************&field4="+str(pressure[0])+"&field5="+str(purity[0])+"&field1="+str(ReadValue[i])+"&field2="+str(mins)+"&field3="+str(timein)))
        print str(ReadValue[30])  
        time.sleep(15)
为True时:
尝试:
除索引器外:
通过

在这种情况下,我们仅在发生
索引器时继续
为了在发生此类错误时继续,只需将希望忽略异常的部分放在适当的
try:。。。除了…

#main program
while True:    

        ReadValue = Func03Modbus(1,70,40);#slave,start,number of registers
        x3 = struct.pack('>HH',abs(ReadValue[3]),abs(ReadValue[2])) 
        pressure = struct.unpack('>f', x3)
        print pressure[0]   
        c3 = struct.pack('>HH',abs(ReadValue[5]),abs(ReadValue[4])) 
        purity = struct.unpack('>f', c3)
        print purity[0] 
        hrs = int(ReadValue[30])
        mins= int(ReadValue[31])
        timein =float(str(ReadValue[30])+"."+str(ReadValue[31]))    
        print timein 
        r=requests.get("http://api.thingspeak.com/update api_key=5RMT************&field4="+str(pressure[0])+"&field5="+str(purity[0])+"&field1="+str(ReadValue[i])+"&field2="+str(mins)+"&field3="+str(timein)))
        print str(ReadValue[30])  
        time.sleep(15)
为True时:
尝试:
除索引器外:
通过

在这种情况下,我们仅在出现
索引器时继续,理论上,您可以将代码包装在异常处理程序中,如:

while True:    
    try:
        <body of work>
    except IndexError:
        <you might want to log the error>
        pass
但是,如果你在与硬件交互,这似乎是一个非常糟糕的主意,因为你不能确定你要让它处于什么状态。理想情况下,您希望确保在每个周期中正确重置设备(或重新连接?取决于您正在与什么通话)

或者,如果要明确验证返回的值是否可用,可以执行以下操作:

while True:
    try:
        what you want to do
    except Exception as e:
        print("Something bad happened:", e)
    finally:
        # reset device here
        time.sleep(15)
ReadValue=Func03Modbus(1,70,40)#从机、启动、寄存器数量
如果len(ReadValue)<32:
打印(“得到不完整的结果”)
时间。睡眠(15)
持续

语言参考/教程在此处提供了有关处理错误的更多信息:

理论上,您可以将代码包装在异常处理程序中,如:

while True:    
    try:
        <body of work>
    except IndexError:
        <you might want to log the error>
        pass
但是,如果你在与硬件交互,这似乎是一个非常糟糕的主意,因为你不能确定你要让它处于什么状态。理想情况下,您希望确保在每个周期中正确重置设备(或重新连接?取决于您正在与什么通话)

或者,如果要明确验证返回的值是否可用,可以执行以下操作:

while True:
    try:
        what you want to do
    except Exception as e:
        print("Something bad happened:", e)
    finally:
        # reset device here
        time.sleep(15)
ReadValue=Func03Modbus(1,70,40)#从机、启动、寄存器数量
如果len(ReadValue)<32:
打印(“得到不完整的结果”)
时间。睡眠(15)
持续

语言参考/教程中有更多关于处理错误的信息:

我建议您尝试在
.pack()
之前添加
print len(ReadValue)
。然后可以确定失败时返回的元素数。我建议您尝试在
.pack()
之前添加
print len(ReadValue)
。然后,您可以确定失败时返回的元素数。“if len(ReadValue)<32:”表示“32”代表什么?是否表示位大小?@fayaz您读取的最高值是
ReadValue[31]
,因此
ReadValue
至少需要32个元素。我没有读取32个值。。。。。。我只是将ReadValue[3]和ReadValue[2]连接起来,并进行一些浮点转换。
ReadValue[31]
意味着您将从该列表中获取第31个元素。您得到的是
索引器
,因为您没有得到所有预期的寄存器。在该错误中,您的
ReadValue[2]
ReadValue[3]
丢失。稍后,您将访问31-
mins=int(ReadValue[31])
“if len(ReadValue)<32:”表示“32”代表什么?是否表示位大小?@fayaz您读取的最高值是
ReadValue[31]
,因此
ReadValue
至少需要32个元素。我没有读取32个值。。。。。。我只是将ReadValue[3]和ReadValue[2]连接起来,并进行一些浮点转换。
ReadValue[31]
意味着您将从该列表中获取第31个元素。您得到的是
索引器
,因为您没有得到所有预期的寄存器。在该错误中,您的
ReadValue[2]
ReadValue[3]
丢失。稍后您将访问31-
mins=int(ReadValue[31])