简体   繁体   中英

Python : IndexError: tuple index out of range

In python i have this code

if record[0][1]:

the problem is.. when mysql does not return anything and thus..

record[0][1]

has no data..

this python code fails:

if record[0][1]:
IndexError: tuple index out of range

i simply want it to move on to the "else" statement or simply consider this if statement as .. invalid given that

record[0][1]

has no value. or data.. ( incoming stuff from mysql )

try:
    if record[0][1]:
        # Do stuff
except IndexError:
    pass

您可以try...except使用try...except或使用外部元组的短路检查。

if len(record) > 0 and len(record[0]) > 1 and record[0][1]:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM