Python 在不停止while循环的情况下中断for循环

Python 在不停止while循环的情况下中断for循环,python,mysql,python-2.7,Python,Mysql,Python 2.7,好的,我不知道如何正确地问这个问题,但我想做的是: #connect to db and stuff check_against = [] #list of strings while row is not None: if row == None: break for i in range(0,len(row)): x = row[i] try: print str(x) except: pass for z in che

好的,我不知道如何正确地问这个问题,但我想做的是:

#connect to db and stuff
check_against = [] #list of strings
while row is not None:
  if row == None:
    break
  for i in range(0,len(row)):
    x = row[i]
    try:
      print str(x)
    except:
      pass
    for z in check_against:
      if z == x:
        #change alternate_id
    if x == "":
      # Change insert_into and stuff
      if alternate_id != 0:
        for x in range(len(insert_into)):  #Do SQL things
          print "updating" + str(alternate_id)
          sql = """UPDATE `table` SET `%s` = '%s' WHERE `table`.`id` = %s""" % (insert_into[x],check_against[x],str(alternate_id))
      try:
         cursor.execute(sql)
         db.commit()
      except:
         db.rollback()
      break #This break breaks the while loop
  row = cursor.fetchone()
如何使中断不是中断while循环而是中断for循环?
谢谢您的帮助。

首先,您不需要此部件:

while row is not None:
    if row == None:
        break
此部分没有任何意义,因为如果行为None,则
while
循环将停止,因为条件
while row not None
,请删除:

if row == None:
    break
你不需要这个代码

第二部分:


如果
中断
在循环时中断了你的
,请以正确的方式格式化你的代码,因为当前代码看起来很奇怪

这正是你的代码已经做过的。。。事实上,你不能在
的时候离开
,因为
x
在它里面永远不会改变。那么我做错了吗?当我在实际代码中使用它时,它打破了while循环。@Tygran,你能提供所有的代码吗?因为当前的实现似乎打破了
for
循环,正如您希望的那样,然后给出一个实际重新创建问题(而不是“所有代码”)的方法,因为这个方法不会。我可以尝试使代码段更像真实代码,因为真实代码非常长。给我一分钟,我想我已经弄明白了。中断不会中断while循环,但问题是我在执行sql时使用的游标与从中获取行的游标相同。谢谢你的帮助。我是哑巴,不客气!问题是关于打破一个while循环,我告诉了如何解决这个问题