[英]IndexError in python
我正在基於Java代碼的python中工作。
我在Java中有這個:
public static byte[] datosOEM = new byte[900000];
public static byte x1=0,x2=0,x3=0,x4=0,x5=0,x6=0;
我用發現的一些文檔用Python編寫了此代碼:
datosOEM=bytes([0x90, 0x00, 0x00])
x1=bytes([0x00])
x2=bytes([0x00])
x3=bytes([0x00])
x4=bytes([0x00])
x5=bytes([0x00])
x6=bytes([0x00])
當我運行程序時,它向我顯示:
Traceback (most recent call last):
File "test.py", line 63, in <module>
x1=datosOEM[k];
IndexError:string index out of range
我該如何解決這個問題 ? 任何幫助都受到好評。
我的代碼的一部分是這樣的:
...
response=port.read(8)
print(response)
k=0
C=0
conexion=True
if(conexion):
while(response>200):
while(C==0):
x1=datosOEM[k];
if(x1=1):
x2=datosOEM[k+1];
...
另外,我該怎么做才能不再重復該錯誤?
預先感謝您的幫助
此帖子由JasonMArcher編輯,非常感謝。
我不確定您要達到什么目標。 也許是這樣嗎?
datosOEM = [0]*900000 # a list of length 900.000, initialized with zeros
x1 = 0
x2 = 0
x3 = 0
# ...
也許您甚至想要這個:
datosOEM = [0]*900000 # a list of length 900.000, initialized with zeros
x = [0]*6
# ...
# ...
while (C==0):
x[0] = datosOEM[k];
if (x[0] = 1):
x[1] = datosOEM[k+1];
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.