繁体   English   中英

如何在 Python MySQL 中跳过一行

[英]How to skip a row in Python MySQL

我有一张表,我想使用 Python 在 MySQL 中编辑,并且只想编辑某些行并跳过其他行。

我需要这个比这个例子更复杂,例如 object 中的时间相关变化,表从中获取数据,但这是我需要的最简单的东西。

cursor.execute("SELECT * FROM given_table")
result = cursor.fetchall()
for row in result:
    whatto = "{Name}".format(Name=row['Action'])
    if whatto == "Skip" :
        #instruction to print row number
        #instruction to skip rest of the loop
    #other instructions    

我如何告诉 Python 跳过一行并给我行号(或者如果我一开始就选择了行号)?

如果我写“print(row)”,它会打印我的行内容,如果我说“print(row[0])”,它会打印我第 1 列的内容。

给我行号

如果您在列表或其他可迭代项中同时需要元素及其“position”,则可以使用enumerate

我如何告诉 Python 跳过一行

continue放置在循环体中,继续最近的封闭循环的下一个循环。

使用您的案例中提到的

for inx, row in enumerate(result):
    whatto = "{Name}".format(Name=row['Action'])
    if whatto == "Skip" :
        print(inx)
        continue
    #other instructions  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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