Easy "here documents" ??

M.E.Farmer mefjr75 at hotmail.com
Sun Dec 19 01:11:26 EST 2004


I was curious so I googled , looks like a unix thing :)

http://www.faqs.org/docs/abs/HTML/here-docs.html

Ok I am with Peter on this , still clueless.
What about string replacement.

py> x = """ Hello me name is ~NAME~. \n I am ~AGE~ years old.\n
...        I live in ~CITY~.\n The city of ~CITY~ is nice.\n
...        I have live here for ~AGE~ years.\n
...     """
py> x = x.replace('~AGE~', '12')
py> x = x.replace('~NAME~', 'Jimmy')
py> x = x.replace('~CITY~', 'Oma')

It makes your template cleaner cause you can use what you want
for the tokens, but takes more lines to do the replacements.
still clueless,
M.E.Farmer


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