I'm trying to make a simple website demo and I have some trouble editing the html using python. The following is the string I defined to store the html (not complete because it's too long). I want to write a function that will throw a error under a certain condition, but I don't know how to insert a line in the string "page".
page="""
Sign up.label {text-align: right}
.error {color: red}
Sign up
Username
Password
I want to insert the line "There is an error" in the blank between these two lines:
Note there are two of this group in the string. How can I insert "There is an error" in the first group using Python? Thanks.
解决方案
python uses C style format strings.
page="""
......
%s
""" % "There was an error"
alternatively, you can use python string.format as
"fooo {x}".format(x="bar").
jinja is an excellent template engine if you've got the time. Genshi (https://genshi.edgewall.org/wiki/GenshiTutorial) is worth looking into as well.
for Jinja2:
pip install Jinja2
in python
from jinja2 import Template
page = Template(""" ....
{{x}}""")
page.render(x="There was an error")