Python xlrd : how to convert an extracted value?

user330860 picture user330860 · Jan 18, 2012 · Viewed 24.8k times · Source

Well i have a question that i feel i've been answered several times, from what i found here. However, as a newbie, i can't really understand how to perform a really basic operation.

Here's the thing :

  • i have an .xls and when i use xlrd to get a value i'm just using sh.cell(0,0) (assuming that sh is my sheet);

  • if what is in the cell is a string i get something like text:u'MyName' and i only want to keep the string 'MyName';

  • if what is in the cell is a number i get something like number:201.0 and i only want to keep the integer 201.

If anyone can indicate me what i should to only extract the value, formatted as i want, thank you.

Answer

comamitc picture comamitc · Jan 18, 2012

sh.cell(x, y) returns an instance of the class Cell. When you print sh.cell(x,y) you are returning the repr function of the class (so it prints type:value).

you should try:

cell = sh.cell(x,y)
print(cell.value)

I cannot test this since I don't have xlrd but, I think it will work given the documentation: https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#sheet.Cell-class