xlrd read number as string

Virgil Balibanu picture Virgil Balibanu · Aug 20, 2015 · Viewed 12.3k times · Source

I am trying to read from an xls file a long number (6425871003976) but python keeps trunking it before it reads it as a number not a string (6.42587100398e+12). Is there any method to read it directly as a string even thou in the xls file it is a number?

values = sheet.row_values(rownum)

in values it appears correctly (6425871003976.0) but when I try values[0] it is already switched to the incorrect value.

Solution:

This was my solution using repr():

if type(values[1]) is float:  
    code_str = repr(values[1]).split(".")[0]
else:
    code_str = values[1]
product_code = code_str.strip(' \t\n\r')

Answer

Dunes picture Dunes · Aug 20, 2015

It's the same value. All that's different is how the value is being printed to screen. The scientific notation you get is because to print the number str is called on it. When you print the list of values the internal __str__ method of the list calls repr on each of its elements. Try print(repr(values[0])) instead.