【问题标题】:How to fix 'list index out of range' error in python?如何修复python中的“列表索引超出范围”错误?
【发布时间】:2019-10-13 18:11:49
【问题描述】:

我正在为考勤系统设置 RFID 阅读器。但是当我将我的标签放到阅读器上时,它被标签 ID 打断了——我在外部存储到数据库中。

#//////place your tag//////

print("Now place your tag to write")
rdr.wait_for_tag()

(error, data) = rdr.request()
if not error:
    print("\nDetected: " + format(data, "02x"))

(error, uid) = rdr.anticoll()
if not error:
    print("Card read UID: " +str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]))

tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 

print("Written..!")
print(tagid)


cursor.execute("insert into rfid_check (uid,firstname,age,tag_id) values ('%s','%s','%s','%s')" %(user_id,fname, ag_e, tagid))
connection.commit()
print("Data was successfully Added...!")

tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 
IndexError: list index out of range

【问题讨论】:

    标签: python arrays python-3.5 sqldatatypes


    【解决方案1】:

    您的代码结构不正确。每当标签请求不正确时,尝试使用此缩进 rdr.anticoll() 不会发生任何过程。

    试试这行代码以获得更好的可视化效果:

    print("Now place your tag to write")
    rdr.wait_for_tag()
    
    (error, data) = rdr.request()
    if not error:
        print("\nDetected: " + format(data, "02x"))
    
        (error, uid) = rdr.anticoll()
        if not error:
            print("Card read UID: " +str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]))
    
            tagid = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) 
    
            print("Written..!")
            print(tagid)
            cursor.execute("insert into rfid_check (uid,firstname,age,tag_id) values ('%s','%s','%s','%s')" %(user_id,fname, ag_e, tagid))
            connection.commit()
            print("Data was successfully Added...!")
    else:
        print("Unsuccessful")
    

    【讨论】:

    • 问题是每当标签不正确而不是不处理不正确的请求时,它仍然会处理不正确的请求
    • 仍然,我有同样的错误 [tap_id = str(uid[0])+str(uid[1])+str(uid[2])+str(uid[3]) IndexError : 列表索引超出范围]
    • 尝试打印uid的长度。我认为获取的数据还不够
    猜你喜欢
    • 2019-06-24
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多