python best way to output larges amount of formatted text -


i'm trying think of best way write large amounts of formatted text in python text file. i'm creating tool generates php files automatically depending on user input.

the way limited knowledge of python can come @ moment write each line individually file so:

print("    <html>", file=f) print("        <head>", file=f) print("        </head>", file=f) print("        <body>", file=f) ..... 

the skeleton of document print file above , add in variables user supplies via raw_input such page title, meta data etc

what better way of doing this?

i suppose looking similar pythons commenting systems this:

""" page contents indentation here + variables """ 

then write out file

what call "python commenting system" not "comment system" - comments start "#" -, it's syntax multiline strings. fact it's used docstrings (which not comments documentation , become attributes of documented object) doesn't make less of proper python string. iow, can use simple templating:

tpl = """ <html>     <head>     </head>     <body>         <h1>hello {name}</h1>     </body> </html> """ print tpl.format(name="world") 

but more involved - conditionals, loops, etc - you'd better use real templating system (jinja being choice).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -