0

I recently asked a question here how can I read each line of a xls file with pausing and I got an answer which solved my problem. The code is as follows

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

what I want to do is to make the loop in a way that goes to the end of the row lines of the xls and then return the beginning over and over until I stop it or pause it for sometimes , do you know how to do this ?

Community
  • 1
  • 1

2 Answers2

2

Just put it inside another loop. For example, if you want to repeat the entire thing 10 times, just do this:

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
bzrr
  • 1,490
  • 3
  • 20
  • 39
  • not really, I am getting error because of the condition Traceback (most recent call last): File "my_prog2.py", line 16, in if condition: NameError: name 'condition' is not defined also it does not clear the terminal – Learner Algorithm Nov 18 '16 at 12:00
  • @LearnerAlgorithm You have to replace `condition` with the actual condition you'd like the loop to exit after it is true. – bzrr Nov 18 '16 at 12:02
2

Probably the easiest way I've found is to use python's KeyboardInterrupt exception - that is of course, if you're OK with your whole script halting when you end the loop (by pressing Ctrl-C).

for sheet in workbook.sheets():
    for row in range(sheet.nrows):
        for column in range(sheet.ncols):
            try:
                os.system('clear')
                print "value::: ", sheet.cell(row,column).value
                time.sleep(5.5)    # pause 5.5 seconds
            except KeyboardInterrupt:
                pass #insert here anything you'd like your script to do before it halts
deefff
  • 333
  • 2
  • 9
  • it gives me an error , I used it like except KeyboardInterrupt: pass Ctrl+C which gives me a syntax error File "my_prog2.py", line 16 pass Ctrl+C#insert here anything you'd like your script to do before it halts ^ SyntaxError: invalid syntax – Learner Algorithm Nov 18 '16 at 15:19
  • Why did you write 'Ctrl+C' in your script? :) `pass` is just a standalone statement that basically says 'do nothing'. Try it after deleting 'Ctrl+C'. In case the comment after `pass` confused you, I just meant that you can substitute `pass` with anything you'd like the script to do upon exiting (writing a good-bye message or closing a file, etc.). – deefff Nov 18 '16 at 15:33
  • as I said, I want it to continue until, I stop it by a keyword or I pause it by a keyword. If I remove the ctrl+C it continues and whatever keyword I push , it does not stop or pause and continue – Learner Algorithm Nov 18 '16 at 15:44
  • What do you mean by keyword? You should be able to halt any process by pressing Ctrl-C (or Ctrl-D). – deefff Nov 19 '16 at 15:13
  • Ctrl-C or D will stop the program. what if I want to pause it and then click another keyboard and start from the time it stoped ? – Learner Algorithm Nov 19 '16 at 21:02
  • Depending on how sophisticated the solution you are looking for, you can either use Ctrl+Z to suspend the process and then start again with `fg` (in the foreground) or `bg` (in the background); or use the [getch](https://pypi.python.org/pypi/getch/1.0) module of python. – deefff Nov 20 '16 at 09:57
  • can you give a solution to this? in a simple way? suspending it with ctrl+Z seems alright but getting it back to continue is not straight forward – Learner Algorithm Nov 20 '16 at 10:13
  • Just type `fg` in the command prompt and hit enter. (It is explained in more detail [here](http://stackoverflow.com/questions/20649783/pause-a-running-script-in-mac-terminal-and-then-resume-later)), although the `fg` command alone should also do the trick. Plus, I realize that you might have to move the `try`-`except` block inside the `while` loop to make it continue upon typing `fg`. I'll edit my answer to reflect that. – deefff Nov 20 '16 at 17:53