-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 13.04.2013 21:03, Eric Smith wrote:
> I appreciate the quick response. I am having trouble getting this
> to work and am most likely doing this wrong. Not sure I
> understand.
>
>
>
> try: cell5 = sheet.cell(5,0) except xlrd.XLRDError: cell5 is
> empty_cell
>
try:
cell5 = sheet.cell(5,0)
except:
do_something_meaningful_here
"do_something_meaningful_here" depends on what you're doing.
If your program MUST read a value from a cell 5:0, and cannot ever
proceed if there's no such cell, then there's really nothing you can
do, and try...except won't help you either.
If you're OK with just having some value (0, for example) for
non-existing cells, then just do:
cell5 = 0
inside the except block.
Usually you have some pretty good idea about the layout of the xls
file, and can rely on that layout staying precisely the same. In which
case an xls that does not have a 5:0 cell is broken (from your
program's point of view), and crashing is the right thing to do.
Or you know what kinds of peciliarities the layout might have, and
what to do if you hit them. For example, if you have values neatly
arranged in a row, usually, but sometimes they are in a row, but with
an empty cell in the middle, you can say "ok, data is probably shifted
to 1 cell to the right, so i'm just going to skip over the empty cell
and continue reading values as if nothing happened". That requires you
to NOT to hard-code cell indices into your program, and maybe use a
loop or something like that.
But all that has nothing to do with xlrd, really, it's just normal
programming, using loops, catching exceptions, etc. Go read some
Python books.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Thunderbird -
http://www.enigmail.net/
iQEcBAEBAgAGBQJRaZOJAAoJEOs4Jb6SI2CwDrgIALjkX5Ifqi2rVo8gOb2HlRZv
yKGcRV+IdKh7D9KqnRWy/82kNlPsxX8KJkCKmuXhOl6ME78LuVITKy6b0Id4WtzP
PbBZckqo7gM8AmXRE2EjHadDmU9Qj9Yr0Al8XNFbehlILZ+s3uo9rofJS/qtSq3I
38kenEYyLx09Wer/h/VBSzfIVOCmvVw4bXeffP4IF+1o5YS5XLTS1ICWl3NhFnm+
DZz+72Klp63b4kQInTyMyNCCcUZSSZJU1i0b68p0EYmHTWQPUnEYXesORcB4CALJ
Tyin+EbjvlfOCmn54pTHUReZ9DqzcrPK/ZrxDNunH0kihQhExH3M+Mc2YNTBfxQ=
=kXho
-----END PGP SIGNATURE-----