Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41927
| Path | csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'elif': 0.05; 'float': 0.07; 'none,': 0.07; 'data:': 0.09; 'expression:': 0.09; 'false.': 0.09; 'formatting': 0.09; 'plug': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:number': 0.09; 'way:': 0.09; 'def': 0.12; '(100,': 0.16; 'evaluates': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'row)': 0.16; 'spec):': 0.16; 'str):': 0.16; 'ternary': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'subject:need': 0.19; 'fit': 0.20; 'header:User-Agent:1': 0.23; 'logical': 0.24; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'rest': 0.29; 'chris': 0.29; 'skip:( 20': 0.30; 'gives': 0.31; 'code': 0.31; 'apparently': 0.31; 'operators': 0.31; 'writes:': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'but': 0.35; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'expression': 0.60; 'tell': 0.60; 'first': 0.61; 'self.value': 0.84; 'subject:nothing': 0.84; 'working,': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: I need a neat way to print nothing or a number |
| Date | Tue, 26 Mar 2013 18:22:21 +0100 |
| Organization | None |
| References | <jms82a-6sl.ln1@chris.zbmc.eu> <CAPTjJmp2-x0Nsw9JtLscF3QfHpYGb9fgCRAvFMf7EwVdWqt24g@mail.gmail.com> <loom.20130326T175824-258@post.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084a8f4.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3759.1364318537.2939.python-list@python.org> (permalink) |
| Lines | 47 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364318537 news.xs4all.nl 6940 [2001:888:2000:d::a6]:38274 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:41927 |
Show key headers only | View raw
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)))
Back to comp.lang.python | Previous | Next — Previous in thread | Next 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