I need a neat way to print nothing or a number

Discussion in 'Python' started by cl@isbd.net, Mar 26, 2013.

  1. Guest

    What's a neat way to print columns of numbers with blanks where a number
    is zero or None?

    E.g. I want to output something like:-

    Credit Debit Description
    100.00 Initial balance
    123.45 Payment for cabbages
    202.00 Telephone bill


    For each line I have either the credit or the debit amount and the other
    is 0 or None. However you can't get number formatting (old or new) to
    output a blank for 0 and it barfs on None.

    --
    Chris Green
    , Mar 26, 2013
    #1
    1. Advertising

  2. On Wed, Mar 27, 2013 at 2:50 AM, <> wrote:
    > What's a neat way to print columns of numbers with blanks where a number
    > is zero or None?
    >
    > E.g. I want to output something like:-
    >
    > Credit Debit Description
    > 100.00 Initial balance
    > 123.45 Payment for cabbages
    > 202.00 Telephone bill
    >
    >
    > For each line I have either the credit or the debit amount and the other
    > is 0 or None. However you can't get number formatting (old or new) to
    > output a blank for 0 and it barfs on None.


    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
    Chris Angelico, Mar 26, 2013
    #2
    1. Advertising

  3. John Gordon Guest

    In <> writes:

    > What's a neat way to print columns of numbers with blanks where a number
    > is zero or None?


    print number or ' '

    --
    John Gordon A is for Amy, who fell down the stairs
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Mar 26, 2013
    #3
  4. 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 '').

    Wolfgang
    Wolfgang Maier, Mar 26, 2013
    #4
  5. Ethan Furman Guest

    On 03/26/2013 10:06 AM, Wolfgang Maier 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 '').


    Use parens then:

    ("%.2f" % value) if value else ''

    --
    ~Ethan~
    Ethan Furman, Mar 26, 2013
    #5
  6. Peter Otten Guest

    Wolfgang Maier 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 '').


    Here's a round-about way:

    class Prepare:
    def __init__(self, value):
    self.value = value
    def __format__(self, spec):
    if self.value is None or self.value == 0:
    return format(0.0, spec).replace(".", " ").replace("0", " ")
    elif isinstance(self.value, str):
    return self.value.rjust(len(format(0.0, spec)))
    return format(self.value, spec)

    def prepare(row):
    return map(Prepare, row)

    data = [
    ("Credit", "Debit", "Description"),
    (100, 0, "Initial balance"),
    (123.45, None, "Payment for cabbages"),
    (0.0, 202.0, "Telephone bill"),
    ]

    for row in data:
    print("{:10.2f} {:10.2f} {}".format(*prepare(row)))
    Peter Otten, Mar 26, 2013
    #6
  7. On Wed, Mar 27, 2013 at 4:21 AM, Ethan Furman <> wrote:
    > On 03/26/2013 10:06 AM, Wolfgang Maier 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 '').

    >
    >
    > Use parens then:
    >
    > ("%.2f" % value) if value else ''


    According to the operator precedence table, the parens are unnecessary there.

    ChrisA
    Chris Angelico, Mar 26, 2013
    #7
  8. 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
    Wolfgang Maier, Mar 27, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. korean_dave
    Replies:
    0
    Views:
    268
    korean_dave
    Jul 24, 2008
  2. Salty Dog
    Replies:
    4
    Views:
    783
    Salty Dog
    Feb 28, 2005
  3. Devin Jeanpierre
    Replies:
    2
    Views:
    427
    Devin Jeanpierre
    Feb 14, 2012
  4. Tim McDaniel
    Replies:
    2
    Views:
    370
    Tim McDaniel
    Aug 27, 2012
  5. hymie!
    Replies:
    1
    Views:
    344
    Tim McDaniel
    Aug 27, 2012
Loading...

Share This Page