Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89879

Re: Best way to use globally format

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'though:': 0.07; 'string': 0.09; 'dependency': 0.09; 'occurrences': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'happy.': 0.16; 'hell.': 0.16; 'nesting': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'skip:{ 30': 0.16; 'subject:format': 0.16; '{0}': 0.16; '{1},': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'skip:{ 20': 0.24; 'header:X-Complaints-To:1': 0.27; 'converting': 0.30; 'way?': 0.31; 'file': 0.32; 'could': 0.34; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'most': 0.60; 'numbers': 0.61; 'first': 0.61; 'of:': 0.68; 'clearer': 0.84; 'subject:Best': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Best way to use globally format
Date Sun, 03 May 2015 18:16:41 +0200
Organization None
References <87wq0pk7pf.fsf@Equus.decebal.nl>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd888f.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.70.1430669817.12865.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430669817 news.xs4all.nl 2946 [2001:888:2000:d::a6]:42616
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89879

Show key headers only | View raw


Cecil Westerhof wrote:

> I have a file where I used a lot of {0}, {1} and {2}. Most but not all
> are changed to {0:.3E}, {1:.3E} and {2:.3E}. But when I want to change
> the format I come in dependency hell.
> 
> I could do something like:
>     format = ':.3E'
>     fmt0   = '{0' + format + '}
>     fmt1   = '{1' + format + '}
>     fmt2   = '{2' + format + '}
> 
> and replace occurrences of:
>     'before {0} after'
> with:
>     'before ' + fmt0 + ' after'
> 
> But that does not really make me happy. Is there a better way?
 
There's limited support for nesting {...}:

>>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt=".3E")
'1.123E+00, 2.000E+00, 3.000E+00'
>>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt="6.2")
'   1.1,    2.0,    3.0'
>>> "{0:{fmt}}, {1:{fmt}}, {2:{fmt}}".format(1.12345789, 2., 3., fmt="06.2")
'0001.1, 0002.0, 0003.0'

Converting the numbers to string first may be clearer though:

>>> formatted_numbers = [format(x, "010.2") for x in [1.12345789, 2., 3.]]
>>> "{0}, {1}, {2}".format(*formatted_numbers)
'00000001.1, 00000002.0, 00000003.0'

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Best way to use globally format Cecil Westerhof <Cecil@decebal.nl> - 2015-05-03 17:21 +0200
  Re: Best way to use globally format Peter Otten <__peter__@web.de> - 2015-05-03 18:16 +0200
  Re: Best way to use globally format Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-03 17:32 +0100

csiph-web