0

I asked a question here how can I read each line of a xls file with pausing and the answer is right as below

This solution goes row wise . for example if I have 3 values at first row. It prints first cell then second cell then third cell etc until the first row finish and then goes to second row.

What I want is to print all cells with a distance at the same time for example print the first row then print the second row etc

As an example, if I have in my xls file two rows as follows

row 1 : 1 2 3 (there are three values in three cell) row 2: 5 6 7

I want it prints it like

value :::  1 ------> 2------> 3 
value ::: 5-------> 6------> 7





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

this shows only one column of xls but if I have two or three columns it ignore the second and the third etc and only print the first one

how can I amend this to be able to print as many cell as I want in each epoch ?

Community
  • 1
  • 1
  • You already import pandas in your script. Why don't you use pandas' own function to turn your excel sheet into a pandas frame and print the relevant columns? – sobek Nov 20 '16 at 10:29
  • @sobek I don't know how to do it :-) can you please give me a solution ? also describe it then I will learn from it – Learner Algorithm Nov 20 '16 at 10:37

1 Answers1

1

This is relatively easy to do with pandas.

import time
import pandas as pd
import os

frame = pd.read_excel('myfile.xls')

This will give you a pandas DataFrame with only the first sheet of that excel file.

pandas dataframes have an inbuilt method to iterate over all rows:

for row in frame.iterrows():
    print row
    time.sleep(5.5)
    os.system('clear')

Please also refer to the documentation of pandas.read_excel (this is for pandas version 0.19.1).

Update:

Well, not properly testing this has come around to bite me in the bottom. It would appear that iterating over single rows of the DataFrame has unexpected side effects on formatting. To make sure this does not happen, the following code transforms the frame into a string via the to_string method that the DataFrame class features. We do not want to get the row index printed out as well so we set the parameter index to false.

To be able to iterate over this data row-wise, we need to split our string at each newline, this is what the splitlines function does. It is an inbuilt function of python strings. It turns our single string containing the entire data frame into a list of strings, each containing only a single line.

pandas separates columns by two spaces, so finally we replace each occurrence of double spaces with the wanted delimiter via the builtin replace function.

import time
import pandas as pd
import os

frame = pd.read_excel('data.xls')

for row in frame.to_string(index=False).splitlines():
    os.system('clear')
    print 'value ::: ' + row.replace('  ', '------>')
    time.sleep(5.5)

os.system('clear')

For reference, also see the python manual on built in types, it lists the methods available for strings. Naturally, these are extremely helpful when trying to manipulate strings.

Update 2:

This is starting to get off topic, but here is a colored version as per your comment.

import time
import pandas as pd
import os

frame = pd.read_excel('data.xls')

blue = '\033[94m'
green = '\033[92m'
yellow = '\033[93m'
plain = '\033[0m'    
colormap = [blue, green, yellow]    
delimiter = '------>'

for row in frame.to_string(index=False).splitlines():
    os.system('clear')
    line = 'value ::: '
    for idx, column in enumerate(row.split('  ')):
        if idx > 0:
            line += delimiter
        line += colormap[idx % 3] + column + plain

    print line
    time.sleep(5.5)

os.system('clear')
sobek
  • 1,386
  • 10
  • 28
  • this will print all rows at once!!! I want each row all cells at each epoch. for example sheet 1, row 1, with a distance as I showed above. Then clear the terminal , then print the second etc etc – Learner Algorithm Nov 20 '16 at 11:23
  • @LearnerAlgorithm Well i'm sorry but your question isn't particularly clear about that... I'll try to come up with something. Btw. when you say print the second, the second what? Second sheet? And what does 'with a distance' mean? When you print a dataframe, it prints the columns with a distance, is that not enough? – sobek Nov 20 '16 at 11:29
  • sorry ! no the second means row . we don't use any other sheet. we stay with only one sheet. the only thing is that we print each row all cells once at the time. for example first row all cells (with a distance from each other) then clear the terminal then print the second row all cells , then clear the terminal , then the third row all cells etc etc . each time a certain pause is given to each row. Please run the script I posted above, you can see what I mean – Learner Algorithm Nov 20 '16 at 11:34
  • @LearnerAlgorithm I've edited the code above, i think this should satisfy your requirements. – sobek Nov 20 '16 at 11:41
  • you dont need my input, you can simply make a xls file with 3 columns of values and 5 rows (randomly) unfortunately your edited code, does not do anything but print all the sheet at once !! – Learner Algorithm Nov 20 '16 at 11:48
  • @LearnerAlgorithm Sorry for the confusion. The latest update should do exactly what you ask. – sobek Nov 20 '16 at 12:17
  • this is already great, I already liked it and I need one more request to ask and then I accept it. I only need to add a sing or something between the cells of each row like `value ::: 1 ------> 2------> 3 `do you know how to do it? – Learner Algorithm Nov 20 '16 at 12:54
  • @LearnerAlgorithm Well that's remarkably easy. `row` is a string. python string objects have a method called `replace`, you simply give it the string to search for and a string to replace it with. See my edit. – sobek Nov 20 '16 at 13:52
  • I accepted your answer but one favour to ask. do you know if I want to color the font in one row the first cell in one color and the second cell in another color ? – Learner Algorithm Nov 20 '16 at 14:49
  • @LearnerAlgorithm See this topic on SO: http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python. I will include this in my answer. – sobek Nov 20 '16 at 14:54
  • I did see that but the problem is the implementation for each cell. I'll appreciate if you could amend your answer , thanks again – Learner Algorithm Nov 20 '16 at 14:59
  • 1
    @LearnerAlgorithm Done. If you're really interested in learning, you should rather try solving yourself and come back with a specific problem when you get stuck. At least try to find out yourself exactly what my code does. You cannot learn to code without actually making mistakes yourself. – sobek Nov 20 '16 at 15:21
  • if you have any suggestion for this problem too, I will appreciate it http://stackoverflow.com/questions/40691707/ioerror-for-python-2-7-and-mac – Learner Algorithm Nov 20 '16 at 17:59