Easy "here documents" ??
Bengt Richter
bokr at oz.net
Mon Dec 20 23:23:49 EST 2004
On Mon, 20 Dec 2004 18:58:09 -0600, Doug Holton <insert at spam.here> wrote:
>> Jim Hill wrote:
>>
>>> Is there a way to produce a very long multiline string of output with
>>> variables' values inserted without having to resort to this wacky
>>>
>>> """v = %s"""%(variable)
>
>No, it is currently not possible in Python without the hacks you have
>seen already. Python is long overdue for simpler string interpolation
>as seen in many other scripting languages.
>
>You should add a feature request to have this in Python 3.0, a.k.a.
>Python 3000: http://www.python.org/cgi-bin/moinmoin/Python3.0
>
>Then you could simple do something like:
>
>variable1 = 1
>variable2 = 2
>
>s = """
> v = ${variable1}
> v2's value is: ${variable2}
>"""
>
>However, Python 3.0 is likely years away. If you want to know how to
>run code like this today, consult Fredrik Lundh.
Or replace ${...} with equally simple %(...)s in the above and be happy ;-)
>>> variable1 = 1
>>> variable2 = 2
>>>
>>> s = """
... v = ${variable1}
... v2's value is: ${variable2}
... """
>>> print s.replace('${','%(').replace('}',')s') % locals()
v = 1
v2's value is: 2
Better would be to write it with %(...)s in the first place though, or you may need
regex help for correct conversion (if involve other uses for the ${} chars).
Regards,
Bengt Richter
More information about the Python-list
mailing list