Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41986
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Subject | Re: I need a neat way to print nothing or a number |
| Date | 2013-03-27 08:23 +0000 |
| References | <jms82a-6sl.ln1@chris.zbmc.eu> <CAPTjJmp2-x0Nsw9JtLscF3QfHpYGb9fgCRAvFMf7EwVdWqt24g@mail.gmail.com> <loom.20130326T175824-258@post.gmane.org> <CAPTjJmrTrBp-nZxKBqQ9GfmvnhJO+OX6vz+DhmAg5hQw515YLA@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3795.1364372634.2939.python-list@python.org> (permalink) |
Chris Angelico <rosuav <at> gmail.com> writes:
>
> On Wed, Mar 27, 2013 at 4:06 AM, Wolfgang Maier
> <wolfgang.maier <at> biologie.uni-freiburg.de> wrote:
> > Chris Angelico <rosuav <at> gmail.com> writes:
> >
> >>
> >> Try printing out this expression:
> >>
> >> "%.2f"%value if value else ''
> >>
> >> Without the rest of your code I can't tell you how to plug that in,
> >> but a ternary expression is a good fit here.
> >>
> >> ChrisA
> >>
> >
> > Unfortunately, that's not working, but gives a TypeError: a float is required
> > when the first value evaluates to False.
> > Apparently it's not that easy to combine number formatting with logical
> > operators - the same happens with my idea ('{:.2f}').format(value or '').
>
> Really? Works for me in 3.3:
>
> >>> value=1.2
> >>> "%.2f"%value if value else ''
> '1.20'
> >>> value=0
> >>> "%.2f"%value if value else ''
> ''
> >>> value=None
> >>> "%.2f"%value if value else ''
> ''
>
> What's the full context? The way I've written the expression, it's
> guaranteed to return a string (either "%.2f"5value or the literal '',
> and yes, I'm aware that I was inconsistent with the quotes).
>
> I tried it in 2.6 and it worked there, too. Now, if you parenthesize
> the bit after the percent sign, the TypeError comes up. But that
> wasn't the intention of the code (and "value if value else
> something-else" is just "value or something-else", anyway).
>
> ChrisA
>
Hi Chris,
yes, I had put parens around your ternary operator expression after the %.
Should have read your code more carefully, but I assumed what you tried to do
was to obtain a *formatted* string in both cases. Your suggestion as it is
really just gives formatting for numbers, but returns an empty string for False
values, so it's just a partial solution to the original problem (basically
converting everything to strings ready for an additional round of formatting).
Anyway, there's a better answer by now, so never mind.
Cheers,
Wolfgang
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
I need a neat way to print nothing or a number cl@isbd.net - 2013-03-26 15:50 +0000 Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 03:08 +1100 Re: I need a neat way to print nothing or a number John Gordon <gordon@panix.com> - 2013-03-26 16:29 +0000 Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-26 17:06 +0000 Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 04:14 +1100 Re: I need a neat way to print nothing or a number Peter Otten <__peter__@web.de> - 2013-03-26 18:22 +0100 Re: I need a neat way to print nothing or a number Ethan Furman <ethan@stoneleaf.us> - 2013-03-26 10:21 -0700 Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 05:48 +1100 Re: I need a neat way to print nothing or a number Tony the Tiger <tony@tiger.invalid> - 2013-03-27 01:11 -0500 Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-27 08:23 +0000
csiph-web