Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'operator': 0.03; "'',": 0.07; 'float': 0.07; 'string': 0.09; 'assumed': 0.09; 'expression:': 0.09; 'false.': 0.09; 'formatting': 0.09; 'literal': 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; '(either': 0.16; 'chris,': 0.16; 'evaluates': 0.16; 'expression,': 0.16; 'message- id:@post.gmane.org': 0.16; 'really?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sign,': 0.16; 'ternary': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'subject:need': 0.19; 'fit': 0.20; 'written': 0.21; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'logical': 0.24; 'mind.': 0.24; 'cheers,': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'converting': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'too.': 0.31; 'apparently': 0.31; 'operators': 0.31; 'received:132': 0.31; 'writes:': 0.31; 'up.': 0.33; 'worked': 0.33; 'there,': 0.34; 'problem': 0.35; "can't": 0.35; 'but': 0.35; 'really': 0.36; '2.6': 0.36; 'false': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'obtain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'easy': 0.60; 'expression': 0.60; 'tell': 0.60; 'full': 0.61; 'first': 0.61; 'more': 0.64; 'mar': 0.68; 'percent': 0.68; 'guaranteed': 0.75; 'partial': 0.84; 'subject:nothing': 0.84; 'working,': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Wolfgang Maier Subject: Re: I need a neat way to print nothing or a number Date: Wed, 27 Mar 2013 08:23:37 +0000 (UTC) References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: sea.gmane.org User-Agent: Loom/3.14 (http://gmane.org/) X-Loom-IP: 132.230.1.31 (Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1364372634 news.xs4all.nl 6883 [2001:888:2000:d::a6]:42177 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41986 Chris Angelico gmail.com> writes: > > On Wed, Mar 27, 2013 at 4:06 AM, Wolfgang Maier > biologie.uni-freiburg.de> wrote: > > Chris Angelico 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