Easy "here documents" ??
vincent wehren
vincent at visualtrans.de
Sun Dec 19 01:15:51 EST 2004
Peter Hansen wrote:
> Jim Hill wrote:
>
>> I've done some Googling around on this and it seems like creating a here
>> document is a bit tricky with Python. Trivial via triple-quoted strings
>> if there's no need for variable interpolation but requiring a long, long
>> formatted arglist via (%s,%s,%s,ad infinitum) if there is. So my
>> question is:
>>
>> 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)
>>
>> business?
>
>
> I have no idea what a "here document" is, but there are several
> alternatives to the "wacky" basic substitution with a tuple of
> values.
OP is looking for "heredoc" syntax; in, let's say, PHP
this lets you do something like:
$foo = new foo();
$name = 'MyName';
echo <<<EOT
My name is "$name". I am printing some $foo->foo.
Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
AFAIK, there is no direct Python equivalent for this kind of syntax.
Using a mapping like you suggested or the string.Template class in
Python 2.4 still maybe improvements over what OP calls that "wacky"
business.
--
Vincent Wehren
>
> The simplest uses a mapping type:
>
> mydict = {'namedVal': 666}
> '''v = %(namedVal)s''' % mydict
>
> Does that let you build whatever a "here document" is?
More information about the Python-list
mailing list