Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #89872 > unrolled thread
| Started by | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| First post | 2015-05-03 17:21 +0200 |
| Last post | 2015-05-03 17:32 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Cecil Westerhof <Cecil@decebal.nl> |
|---|---|
| Date | 2015-05-03 17:21 +0200 |
| Subject | Best way to use globally format |
| Message-ID | <87wq0pk7pf.fsf@Equus.decebal.nl> |
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?
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
[toc] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2015-05-03 18:16 +0200 |
| Message-ID | <mailman.70.1430669817.12865.python-list@python.org> |
| In reply to | #89872 |
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'
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-05-03 17:32 +0100 |
| Message-ID | <87a8xlzkng.fsf@bsb.me.uk> |
| In reply to | #89872 |
Cecil Westerhof <Cecil@decebal.nl> writes:
> 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?
'Better' is often a bit tricky. You could always factor the formatted
printing into a function, but you will have considered that. You could
pass the format to used to the format function:
format_for_numbers = '.3E'
...
'x = {0:{nfmt}}, y = {1:{nfmt}}'.format(3.4, 4.5, nfmt=format_for_numbers);
or you could do that with just the width (if that is the variable part):
'x = {0:.{nwd}E}, y = {1:.{nwd}E}'.format(3.4, 4.5, nwd=4);
--
Ben.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web