Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
xlrd represents numbers with 0 decimal places as floats
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jacob Fenwick  
View profile  
 More options Oct 18, 7:21 am
From: Jacob Fenwick <jacob.fenw...@gmail.com>
Date: Sat, 17 Oct 2009 17:21:59 -0400
Local: Sun, Oct 18 2009 7:21 am
Subject: xlrd represents numbers with 0 decimal places as floats

Hi,

I'm a new user of xlrd.

I was just noticing that any field in Excel represented as number will be
interpreted by xlrd as a python float then has one decimal point. For
example 4 would be represented as 4.0.

I realize this is easy to fix in any python code by converting to an int and
then converting to a str, but I'm wondering if there's a way to change how
this works in xlrd. Perhaps a number with 0 decimal places should return an
int, or at least it should do the conversion to an int so the user doesn't
have to.

Jacob


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 18, 9:37 am
From: John Machin <sjmac...@lexicon.net>
Date: Sun, 18 Oct 2009 10:37:27 +1100
Local: Sun, Oct 18 2009 9:37 am
Subject: Re: [pyxl] xlrd represents numbers with 0 decimal places as floats
On 18/10/2009 8:21 AM, Jacob Fenwick wrote:

> I'm a new user of xlrd.

Welcome ...

> I was just noticing that any field in Excel represented as number will
> be interpreted by xlrd as a python float then has one decimal point. For
> example 4 would be represented as 4.0.

As far as Excel's calculations are concerned, there is no distinction
between a float with zero fractional part and an integer. Excel does
save space in the XLS file where a number can be represented exactly as
a twos-complement 30-bit integer, but this is not mandatory and on
re-reading by Excel so-represented numbers are converted to float and
their history is forgotten.

"one decimal point"? Did you mean "one decimal place"? Floats don't have
  decimal places. Excel's 4 is represented as an IEEE 794 64-bit float,
not "4.0".

> I realize this is easy to fix in any python code by converting to an int
> and then converting to a str,

What is the problem that you say needs fixing?

> but I'm wondering if there's a way to
> change how this works in xlrd. Perhaps a number with 0 decimal places
> should return an int, or at least it should do the conversion to an int
> so the user doesn't have to.

You write as though "return an int" and "do the conversion to an int"
are different -- please explain.

Consider a worksheet that is valuing your stock exchange investments. It
will have columns for quantity, price, and market value. Yes, all of the
quantity numbers are expected to be integers. However the price and MV
columns contain floats, some of which may have a zero fractional part.
Do you really want a display treatment for a number that's determined
programatically without inspecting its neighbours?

HTH,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jacob Fenwick  
View profile  
 More options Oct 18, 10:38 am
From: Jacob Fenwick <jacob.fenw...@gmail.com>
Date: Sat, 17 Oct 2009 20:38:49 -0400
Local: Sun, Oct 18 2009 10:38 am
Subject: Re: [pyxl] Re: xlrd represents numbers with 0 decimal places as floats

I'll start by saying sorry about some of the mistakes I had in my last
email, it was written hastily.

On Sat, Oct 17, 2009 at 7:37 PM, John Machin <sjmac...@lexicon.net> wrote:
> As far as Excel's calculations are concerned, there is no distinction
> between a float with zero fractional part and an integer. Excel does
> save space in the XLS file where a number can be represented exactly as
> a twos-complement 30-bit integer, but this is not mandatory and on
> re-reading by Excel so-represented numbers are converted to float and
> their history is forgotten.

Now that you've explained that Excel actually stores all numbers as floats,
that gives me a better understanding of why this is happening.

> I realize this is easy to fix in any python code by converting to an int
> > and then converting to a str,

> What is the problem that you say needs fixing?

The problem I'm referring to is the discrepancy between how Excel represents
numbers to the user and how I was seeing Python represent them after parsing
them using xlrd. However, I now understand this is just how Excel stores
numbers.

But even if that's how Excel stores numbers, there must be some place that
it's storing that metadata about how many decimal places a field should
represent to the user. Is that information easily accessible?

> Consider a worksheet that is valuing your stock exchange investments. It
> will have columns for quantity, price, and market value. Yes, all of the
> quantity numbers are expected to be integers. However the price and MV
> columns contain floats, some of which may have a zero fractional part.
> Do you really want a display treatment for a number that's determined
> programatically without inspecting its neighbours?

I understand that a quantity should be an integer, while a price could be a
dollar amount represented as a float, but I'm not sure about what market
value is, and I'm not sure how the three would be related, so I'm not sure I
understand the point of your example.

Thanks for your quick reply,

Jacob


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 18, 12:25 pm
From: John Machin <sjmac...@lexicon.net>
Date: Sun, 18 Oct 2009 13:25:37 +1100
Local: Sun, Oct 18 2009 12:25 pm
Subject: Re: [pyxl] Re: xlrd represents numbers with 0 decimal places as floats
On 18/10/2009 11:38 AM, Jacob Fenwick wrote:

It is storing a "number format" string which corresponds to what you see
when you do Format/Cells/Number in the Excel UI. That can be retrieved
by opening the workbook with formatting_info=True and following this
path from a cell's xf_index:

xf = Book.xf_list[xf_index]
fmt_key = xf.format_key
fmt_obj = Book.format_map[fmt_key]
fmt_str = fmt_obj.format_str

xlrd analyses these format strings only as far as attempting to guess if
  the number in a cell is intended to represent a date. Nobody has
written a general-purpose format string parser.

>     Consider a worksheet that is valuing your stock exchange investments. It
>     will have columns for quantity, price, and market value. Yes, all of the
>     quantity numbers are expected to be integers. However the price and MV
>     columns contain floats, some of which may have a zero fractional part.
>     Do you really want a display treatment for a number that's determined
>     programatically without inspecting its neighbours?

> I understand that a quantity should be an integer, while a price could
> be a dollar amount represented as a float, but I'm not sure about what
> market value is, and I'm not sure how the three would be related, so I'm
> not sure I understand the point of your example.

quantity * price -> market_value

123 * 4.56 -> 560.88

123 * 1.00 -> 123.00
123 * 1    -> 123 # you don't want to display it like this

124 * 1.25 -> 155.00
124 * 1.25 -> 155 # you don't want to display it like this

The point is that displaying a whole-number float as an integer
unconditionally is not a good idea.

Cheers,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 21, 12:42 am
From: Chris Withers <ch...@simplistix.co.uk>
Date: Tue, 20 Oct 2009 15:42:52 +0100
Local: Wed, Oct 21 2009 12:42 am
Subject: Re: [pyxl] Re: xlrd represents numbers with 0 decimal places as floats

John Machin wrote:
> when you do Format/Cells/Number in the Excel UI. That can be retrieved
> by opening the workbook with formatting_info=True and following this
> path from a cell's xf_index:

> xf = Book.xf_list[xf_index]
> fmt_key = xf.format_key
> fmt_obj = Book.format_map[fmt_key]
> fmt_str = fmt_obj.format_str

Given the shim-ish nature of Cell objects already, any chance they could
grow a:

   @property
   def xf(self):
     return self.sheet.book.xf_list[self.xf_index]

(I know the .sheet.book bit is hand waving ;-) )

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 21, 8:57 am
From: John Machin <sjmac...@lexicon.net>
Date: Wed, 21 Oct 2009 09:57:02 +1100
Local: Wed, Oct 21 2009 8:57 am
Subject: Re: [pyxl] Re: xlrd represents numbers with 0 decimal places as floats
On 21/10/2009 1:42 AM, Chris Withers wrote:

and thus can't be implemented as described.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 28, 3:04 am
From: Chris Withers <ch...@simplistix.co.uk>
Date: Tue, 27 Oct 2009 16:04:58 +0000
Local: Wed, Oct 28 2009 3:04 am
Subject: Re: [pyxl] Re: xlrd represents numbers with 0 decimal places as floats

How come? Even if the cell doesn't store a reference back to the sheet
it came from, it can't be hard to add ;-)

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google