我有以下代码。
for row in table.rows:
for cell in row.cells:
if cell.tables:
<some code>
else:
<different code>
但是我需要将其写在下面的一行中,以使<different code>
inelse
只能运行一次,而不会因循环而运行多次。
if any(cell.tables for cell in row.cells for row in table.rows):
<some code>
else:
<different code>
但是,此衬板在row.cells处显示未解决的参考“行”错误。此衬纸有效,但不知道为什么在此docx表案例中不使用它。如果条件允许的话,我可以公开提出建议,如果可以用一种更好的方法来做到这一点。
您的for
条款反了。您尝试row
在第一for
个子句中引用,然后在第二个子句中定义它。这就是为什么它不起作用。
你的意思是
if any(cell.tables for row in table.rows for cell in row.cells):
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句