3

I have a xls file on my desktop in mac which has many row (each row consists of a word). what I want is to show each line for 3 minutes in terminal. note that the version of xls is the 2016.

Thanks to How to get line number in excel sheet using python?

import pandas as pd
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):
            print "row::::: ", row
            print "column:: ", column
            print "value::: ", sheet.cell(row,column).value

# then I read my file in 
df = pd.read_excel(path + filename)

Then I know that I can use something like below to

import time
print("something")
time.sleep(5.5)    # pause 5.5 seconds
print("something")

But I could not figure out how to do it without write print , any help is greatly appreciated

Community
  • 1
  • 1

2 Answers2

3

do it in this way on Mac

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
  • thanks , I asked another question, if you have an idea please shoot here http://stackoverflow.com/questions/40676234/how-to-make-a-loop-over-and-over-until-we-stop-or-pause-it – Learner Algorithm Nov 18 '16 at 11:42
1

If I understand this correctly, your problem is that you want a line to show up on the terminal for some time, but get hidden when the next one shows up. You can try this for python3 (see the accepted answer for earlier versions):

import time
import subprocess

import pandas as pd
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):
            subprocess.run(["clear"])
            print "row::::: ", row
            print "column:: ", column
            print "value::: ", sheet.cell(row,column).value
            time.sleep(5.5)    # pause 5.5 seconds

clear is generally for Unix though, if it does not work on a Mac, this answer can be helpful.

Community
  • 1
  • 1
deefff
  • 333
  • 2
  • 9
  • I got this error Traceback (most recent call last): File "my_prog.py", line 10, in subprocess.run(["clear"]) AttributeError: 'module' object has no attribute 'run' – Learner Algorithm Nov 18 '16 at 10:41
  • I liked your answer, thanks but I get error with it – Learner Algorithm Nov 18 '16 at 10:55
  • Yeah, sry, I forgot to mention, but `subprocess.run` is actually the preferred way to call external subprocesses (like shell commands) in `python3`. In earlier versions, like `python2.7` (which you probably use), `os.system` is applicable, as suggested above. – deefff Nov 18 '16 at 11:33
  • thanks , I asked another question, if you have an idea please shoot here http://stackoverflow.com/questions/40676234/how-to-make-a-loop-over-and-over-until-we-stop-or-pause-it – Learner Algorithm Nov 18 '16 at 11:41