Pre-defining an action to take when an expected error occurs

John Machin sjmachin at lexicon.net
Fri Sep 15 08:59:29 CEST 2006


Tempo wrote:
> Thanks for all of the help. It all has been very useful to an new
> python programmer. I agree that I should fix the error/bug instead of
> handeling it with a try/etc. However, I do not know why
> "range(sh.nrows)" never gets the right amount of rows right. For
> example, if the Excel sheet has 10 rows with data in them, the
> statement "range(sh.nrows)" should build the list of numbers [0,
> 1,...9]. It should, but it doesn't do that. What it does is buld a list
> from [0, 1...20] or more or a little less, but the point is that it
> always grabs empy rows after the last row containing data. Why is that?
> I have no idea why, but I do know that that is what is producing the
> error I am getting. Thanks again for the responses that I have received
> already, and again thanks for any further help. Thanks you.

So the xlrd package's Book.Sheet.nrows allegedly "never gets the right
amount of rows right"? Never?? Before making such rash statements in a
public forum [1], you might like to check exactly what you have in your
file. Here's how:

(1) Using OpenOffice.org Calc or Gnumeric (or Excel if you must), open
yourfile.xls and save it as yourfile.csv. Inspect yourfile.csv

(2) Use the runxlrd script that's supplied with xlrd:

    runxlrd.py show yourfile.xls >yourfile_show.txt

Inspect yourfile_show.txt. You'll see things like:
cell A23: type=1, data: u'ascii'
cell B23: type=0, data: ''
cell C23: type=1, data: u'123456'
cell D23: type=0, data: ''
cell E23: type=4, data: 0
The cell-type numbers are in the docs, but briefly: 0 is empty cell, 1
is text, 2 is number, 3 is date, 4 is boolean, 5 is error. If you find
only type=0 in the last row, then indeed you have found a bug and
should report it to the package author (together with a file that
exhibits the problem).

You are likely to find that there are cells containing zero-length
strings, or strings that contain spaces. They *do* contain data, as
opposed to empty cells.

[1] There's a possibility that the package's author reads this
newsgroup, and I've heard tell that he's a cranky old so-and-so; you
wouldn't want him to take umbrage, would you?

HTH,
John




More information about the Python-list mailing list