Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #40130 > unrolled thread
| Started by | Helmut Jarausch <jarausch@skynet.be> |
|---|---|
| First post | 2013-02-28 14:11 +0000 |
| Last post | 2013-02-28 15:06 -0800 |
| Articles | 9 — 5 participants |
Back to article view | Back to comp.lang.python
raw format string in string format method? Helmut Jarausch <jarausch@skynet.be> - 2013-02-28 14:11 +0000
Re: raw format string in string format method? Chris Angelico <rosuav@gmail.com> - 2013-03-01 01:22 +1100
Re: raw format string in string format method? Helmut Jarausch <jarausch@skynet.be> - 2013-02-28 14:42 +0000
Re: raw format string in string format method? Chris Angelico <rosuav@gmail.com> - 2013-03-01 02:09 +1100
Re: raw format string in string format method? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-28 15:27 -0800
Re: raw format string in string format method? Chris Angelico <rosuav@gmail.com> - 2013-03-01 15:29 +1100
Re: raw format string in string format method? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-03-01 00:00 +0000
Re: raw format string in string format method? Peter Otten <__peter__@web.de> - 2013-02-28 16:41 +0100
Re: raw format string in string format method? Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-28 15:06 -0800
| From | Helmut Jarausch <jarausch@skynet.be> |
|---|---|
| Date | 2013-02-28 14:11 +0000 |
| Subject | raw format string in string format method? |
| Message-ID | <512f6585$0$3108$ba620e4c@news.skynet.be> |
Hi,
I'd like to print a string with the string format method which uses
{0}, ...
Unfortunately, the string contains TeX commands which use lots of
braces. Therefore I would have to double all these braces just for the
format method which makes the string hardly readable.
Is there anything like a "raw" format string and any other means
to circumvent this?
Many thanks for a hint,
Helmut.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-01 01:22 +1100 |
| Message-ID | <mailman.2646.1362061370.2939.python-list@python.org> |
| In reply to | #40130 |
On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <jarausch@skynet.be> wrote:
> Hi,
>
> I'd like to print a string with the string format method which uses
> {0}, ...
>
> Unfortunately, the string contains TeX commands which use lots of
> braces. Therefore I would have to double all these braces just for the
> format method which makes the string hardly readable.
>
> Is there anything like a "raw" format string and any other means
> to circumvent this?
You could use a different string formatting function, such as
percent-formatting:
"Hello, {0}, this is %s" % some_string
The {0} will be output literally, and the %s will be replaced by the
string. Braces are ignored, percent signs are significant.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Helmut Jarausch <jarausch@skynet.be> |
|---|---|
| Date | 2013-02-28 14:42 +0000 |
| Message-ID | <512f6cd7$0$3108$ba620e4c@news.skynet.be> |
| In reply to | #40131 |
On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:
> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <jarausch@skynet.be>
> wrote:
>> Hi,
>>
>> I'd like to print a string with the string format method which uses
>> {0}, ...
>>
>> Unfortunately, the string contains TeX commands which use lots of
>> braces. Therefore I would have to double all these braces just for the
>> format method which makes the string hardly readable.
>>
>> Is there anything like a "raw" format string and any other means to
>> circumvent this?
>
> You could use a different string formatting function, such as
> percent-formatting:
>
> "Hello, {0}, this is %s" % some_string
>
> The {0} will be output literally, and the %s will be replaced by the
> string. Braces are ignored, percent signs are significant.
>
> ChrisA
Originally I had used percent-formatting
But isn't it deprecated in Python 3.X ?
Thanks,
Helmut.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-01 02:09 +1100 |
| Message-ID | <mailman.2647.1362064180.2939.python-list@python.org> |
| In reply to | #40132 |
On Fri, Mar 1, 2013 at 1:42 AM, Helmut Jarausch <jarausch@skynet.be> wrote:
> On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:
>
>> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <jarausch@skynet.be>
>> wrote:
>>> Hi,
>>>
>>> I'd like to print a string with the string format method which uses
>>> {0}, ...
>>>
>>> Unfortunately, the string contains TeX commands which use lots of
>>> braces. Therefore I would have to double all these braces just for the
>>> format method which makes the string hardly readable.
>>>
>>> Is there anything like a "raw" format string and any other means to
>>> circumvent this?
>>
>> You could use a different string formatting function, such as
>> percent-formatting:
>>
>> "Hello, {0}, this is %s" % some_string
>>
>> The {0} will be output literally, and the %s will be replaced by the
>> string. Braces are ignored, percent signs are significant.
>>
>> ChrisA
>
> Originally I had used percent-formatting
> But isn't it deprecated in Python 3.X ?
No, it's still fully supported. Every now and then someone suggests
that it's worse than .format() for some reason or another, but the
fact is that it's not going away. Use whichever one makes the most
sense.
Yes, this is a slight violation of "one obvious way to do it". But
it's handy to have both format methods; they have overlapping but
distinct feature sets.
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-02-28 15:27 -0800 |
| Message-ID | <acf73978-d332-4b57-8094-4fe5c52a669d@googlegroups.com> |
| In reply to | #40132 |
On Thursday, February 28, 2013 8:42:31 AM UTC-6, Helmut Jarausch wrote:
> Originally I had used percent-formatting
> But isn't it deprecated in Python 3.X ?
I don't know the current state of "percent formats" future, however, i can tell you that it should be deprecated ASAP!
Simpleminded Sam blubbered: """ But rick, how will we format strings that contain curly braces without the "percent format" option? We can't live without "percent formatting", we are but simple minded folks! *sky-falling* """
Easy Sam. You allow the user to pass "opener" and "closer" chars to the format function.
>>> "Elvis {has} <0> the {{{ building".format('left', opener='<', closer='>')
'Elvis {has} left the {{{ building'
For strings that contain all braces you could even create something unique:
>>> "(Elvis) {has} {S}0{E} the <building>".format('left', opener='{S}', closer='{E}')
'(Elvis) {has} left the <building>'
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-03-01 15:29 +1100 |
| Message-ID | <mailman.2701.1362112158.2939.python-list@python.org> |
| In reply to | #40201 |
On Fri, Mar 1, 2013 at 10:27 AM, Rick Johnson <rantingrickjohnson@gmail.com> wrote: > On Thursday, February 28, 2013 8:42:31 AM UTC-6, Helmut Jarausch wrote: > >> Originally I had used percent-formatting >> But isn't it deprecated in Python 3.X ? > > I don't know the current state of "percent formats" future, however, i can tell you that it should be deprecated ASAP! On Fri, Mar 1, 2013 at 11:00 AM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote: > Nope please see > http://mail.python.org/pipermail/python-dev/2012-February/116790.html And there, folks, you have it. The known troll demands its removal, a core dev says that it's staying. Percent formatting is a viable parallel to .format(), with some handy differences - such as the distinction of significant characters, as proven here. Both are going to stay, and I'm glad of it! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-03-01 00:00 +0000 |
| Message-ID | <mailman.2693.1362095961.2939.python-list@python.org> |
| In reply to | #40132 |
On 28/02/2013 14:42, Helmut Jarausch wrote:
> On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:
>
>> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch <jarausch@skynet.be>
>> wrote:
>>> Hi,
>>>
>>> I'd like to print a string with the string format method which uses
>>> {0}, ...
>>>
>>> Unfortunately, the string contains TeX commands which use lots of
>>> braces. Therefore I would have to double all these braces just for the
>>> format method which makes the string hardly readable.
>>>
>>> Is there anything like a "raw" format string and any other means to
>>> circumvent this?
>>
>> You could use a different string formatting function, such as
>> percent-formatting:
>>
>> "Hello, {0}, this is %s" % some_string
>>
>> The {0} will be output literally, and the %s will be replaced by the
>> string. Braces are ignored, percent signs are significant.
>>
>> ChrisA
>
> Originally I had used percent-formatting
> But isn't it deprecated in Python 3.X ?
>
> Thanks,
> Helmut.
>
Nope please see
http://mail.python.org/pipermail/python-dev/2012-February/116790.html
--
Cheers.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2013-02-28 16:41 +0100 |
| Message-ID | <mailman.2648.1362066115.2939.python-list@python.org> |
| In reply to | #40130 |
Helmut Jarausch wrote:
> I'd like to print a string with the string format method which uses
> {0}, ...
>
> Unfortunately, the string contains TeX commands which use lots of
> braces. Therefore I would have to double all these braces just for the
> format method which makes the string hardly readable.
>
> Is there anything like a "raw" format string and any other means
> to circumvent this?
>
> Many thanks for a hint,
> Helmut.
While percent-formatting is probably the way to go here is a hack to replace
the braces with custom characters:
import string
class SwapFormatter(string.Formatter):
custom = "<>"
braces = "{}"
swap = {ord(p): q for p, q in zip(custom + braces, braces + custom)}
def format(self, format_string, *args, **kw):
format_string = format_string.translate(self.swap)
return super().format(format_string, *args, **kw)
def parse(self, format_string):
return ((t[0].translate(self.swap),) + t[1:]
for t in super().parse(format_string))
myformat = SwapFormatter().format
print(myformat("<< {} {{<0>}} <2> {<foo>} <1:<width>> >>",
"alpha", 42, "gamma", foo="delta", width=5))
[toc] | [prev] | [next] | [standalone]
| From | Rick Johnson <rantingrickjohnson@gmail.com> |
|---|---|
| Date | 2013-02-28 15:06 -0800 |
| Message-ID | <e466cd5b-e65f-458b-98bc-aa6379066038@googlegroups.com> |
| In reply to | #40130 |
On Thursday, February 28, 2013 8:11:17 AM UTC-6, Helmut Jarausch wrote:
> Hi,
>
>
>
> I'd like to print a string with the string format method which uses
> {0}, ...
/What/ uses "{0}" exactly? The substring you wish to inject or the format method? If the latter, we are aware of that!
> Unfortunately, the string contains TeX commands which use lots of
> braces. Therefore I would have to double all these braces just for the
> format method which makes the string hardly readable.
>>> "{0} balls".format("howdy{0} {doody}{10} had {wooden}")
'howdy{0} {doody}{10} had {wooden} balls'
But then why even bother to use the format method? Seriously, if you expect people to give a proper answer you need to not only explain the problem, but also, supply a simplified code example.
> Is there anything like a "raw" format string and any other means
> to circumvent this?
Even if there is, why would someone offer advice from such ambiguous input? Just because a "raw" format exists does not mean that /every/ poorly described problem will be solved using it.
Please provide adequate info.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web