我最近在这里问了一个问题how can I read each line of a xls file with pausing,我找到了解决问题的答案。代码如下
import time
import pandas as pd
import os
import xlrd
# at first I try to know how many rows and how many columns I have
workbook = xlrd.open_workbook('myfile.xls')
for sheet in workbook.sheets():
for row in range(sheet.nrows):
for column in range(sheet.ncols):
os.system('clear')
print "value::: ", sheet.cell(row,column).value
time.sleep(5.5) # pause 5.5 seconds
我想要做的是以一种方式进行循环,直到xls的行行结束,然后一遍又一遍地返回开头,直到我停止或有时暂停它,你知道吗?去做这个 ?
答案 0 :(得分:2)
把它放在另一个循环中。例如,如果你想重复整个事情10次,那就这样做:
for i in xrange(10):
for sheet in workbook.sheets():
num_of_cells = sheet.nrows * sheet.ncols
for cell in range(num_of_cells):
row = int(cell / sheet.ncols)
col = cell % sheet.ncols
print "value::: ", sheet.cell(row, col).value
time.sleep(5.5) # pause 5.5 seconds
答案 1 :(得分:2)
我发现最简单的方法可能就是使用python
的{{1}}例外 - 当然,如果您在结束循环时整个脚本停止运行(通过按Ctrl-C)。
KeyboardInterrupt