python 条件中断循环
python breaking cycle with condition
我正在尝试阅读 xlsx 文档、查找特定通道并打印一个区域,但是 w/o 打印 None-通道。当 "None" 在特定列中时也打破循环。
def some():
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'this one':
for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
max_row=r + 30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
return
else:
print(cell.value, end=" ")
print()
这段代码returns唯一的1道然后break head cycle。
输出:
1315 条文字.
文档格式:https://drive.google.com/file/d/0B_bbUxPgcVESVGstWWRxV3k4emM/view。我无法解决这个问题。请原谅本机错误。我是新手 python,正在寻找答案。
当您检查循环是 None
时,您可以调用 break
退出内部循环,然后在 [=27= 之前有进一步的逻辑,而不是 returning ].您还可以使用 continue
跳过该 for 循环。有关打破内部循环的示例,请参见下文。
示例;如果 cell.value
是 None
:
,这将停止 for cell in rows:
循环
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'My specific Value':
for rows in ws.iter_rows(min_row=r+1, min_col=c-1, max_row=r+30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
rtn = 1
break
else:
print(cell.value, end=" ")
print()
return rtn
第 2 个示例基于突破外部 for 循环(循环的 r 范围):
def print_while_cell_is_not_none():
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'My specific Value':
for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
max_row=r + 30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
return
else:
print(cell.value, end=" ")
第二个示例会破坏整个方法,从而跳出 for r in range
for 循环。
我正在尝试阅读 xlsx 文档、查找特定通道并打印一个区域,但是 w/o 打印 None-通道。当 "None" 在特定列中时也打破循环。
def some():
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'this one':
for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
max_row=r + 30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
return
else:
print(cell.value, end=" ")
print()
这段代码returns唯一的1道然后break head cycle。
输出:
1315 条文字.
文档格式:https://drive.google.com/file/d/0B_bbUxPgcVESVGstWWRxV3k4emM/view。我无法解决这个问题。请原谅本机错误。我是新手 python,正在寻找答案。
当您检查循环是 None
时,您可以调用 break
退出内部循环,然后在 [=27= 之前有进一步的逻辑,而不是 returning ].您还可以使用 continue
跳过该 for 循环。有关打破内部循环的示例,请参见下文。
示例;如果 cell.value
是 None
:
for cell in rows:
循环
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'My specific Value':
for rows in ws.iter_rows(min_row=r+1, min_col=c-1, max_row=r+30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
rtn = 1
break
else:
print(cell.value, end=" ")
print()
return rtn
第 2 个示例基于突破外部 for 循环(循环的 r 范围):
def print_while_cell_is_not_none():
for r in range(1, ws.max_row):
for c in range(1, ws.max_column):
db = ws.cell(row=r, column=c)
if db.value == 'My specific Value':
for rows in ws.iter_rows(min_row=r + 1, min_col=c - 1,
max_row=r + 30, max_col=c):
for cell in rows:
if cell.value is None:
if column_index_from_string(db.column) == c:
return
else:
print(cell.value, end=" ")
第二个示例会破坏整个方法,从而跳出 for r in range
for 循环。