I need a neat way to print nothing or a number

C

cl

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.
 
C

Chris Angelico

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
 
W

Wolfgang Maier

Chris Angelico said:
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
 
E

Ethan Furman

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 ''
 
P

Peter Otten

Wolfgang said:
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)))
 
W

Wolfgang Maier

Chris Angelico said:
Really? Works for me in 3.3:

''

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top