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


Groups > comp.lang.python > #40134

Re: raw format string in string format method?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '*args,': 0.07; 'braces': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:method': 0.09; 'subject:string': 0.09; 'def': 0.10; 'skip:f 30': 0.15; '"<>"': 0.16; '42,': 0.16; 'braces,': 0.16; 'characters:': 0.16; 'circumvent': 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; 'subject:raw': 0.16; 'string': 0.17; 'wrote:': 0.17; 'hack': 0.18; 'import': 0.21; "i'd": 0.22; 'header:User-Agent:1': 0.26; 'replace': 0.27; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'skip:( 20': 0.28; 'probably': 0.29; 'class': 0.29; 'print': 0.32; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'subject:?': 0.35; 'there': 0.35; 'received:org': 0.36; 'method': 0.36; 'anything': 0.36; 'uses': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'here': 0.65; 'therefore': 0.65; 'hardly': 0.84; 'tex': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: raw format string in string format method?
Date Thu, 28 Feb 2013 16:41:54 +0100
Organization None
References <512f6585$0$3108$ba620e4c@news.skynet.be>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849ea1.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.2648.1362066115.2939.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1362066115 news.xs4all.nl 6850 [2001:888:2000:d::a6]:43399
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:40134

Show key headers only | View raw


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))

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


Thread

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

csiph-web