【发布时间】:2019-12-22 04:51:54
【问题描述】:
如何让for循环中,每执行7次循环后,执行某个动作,然后循环继续执行7次?
现在我有一个循环使用 Python + applescript 执行一些操作。 我尝试使用 while 来创建一个应该在每 7 步后执行的条件,然后使用 excel 继续循环。循环正常,一切正常。但是在while开始执行的那一刻——它不想从头开始循环,它想继续执行while,因为count变量从excel文件中获取行。 如何解决?
from subprocess import Popen, PIPE
import xlrd
def main():
apple_script = '''
code
'''
workbook = xlrd.open_workbook('test.xlsx')
sheet = workbook.sheet_by_index(0)
count = 1
for cell in sheet.col(0):
p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stdout, stderr = p.communicate(apple_script)
count += 1
while count >= 7:
app_script = '''
code
'''
p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stdout, stderr = p.communicate(app_script)
break
if __name__ == "__main__":
main()
【问题讨论】:
-
你听说过模数吗?我认为这可以帮助你每七次做一些事情。
标签: python python-3.x for-loop while-loop applescript