社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

用python格式化html

Hoseong Jeon • 6 年前 • 1728 次点击  

我想用python格式化我的html代码。

我的python文件是:

titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
html_text = """<html>
<head>
    <style type="text/css">
        table { border-collapse: collapse;}
        td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }
    </style>
</head>
<body>
    <table width="100%" height="100%" border="5px">
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
    </table>
</body>
</html> % (titles[0], titles[1], titles[2], titles[3], titles[4])"""

f = open('temp.html', 'w')
f.write(html_text)
f.close()

我想使这些%s成为标题[0]、标题[1]、标题[2]、标题[3]、标题[4]。

我怎样才能做到呢?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/43687
 
1728 次点击  
文章 [ 4 ]  |  最新文章 6 年前
Tom Wojcik
Reply   •   1 楼
Tom Wojcik    6 年前

fstrings 现在很酷的孩子用的就是这个。

titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
html_text = f"""<html>
<head>
    <style type="text/css">
        table {{ border-collapse: collapse;}}
        td {{ text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }}
    </style>
</head>
<body>
    <table width="100%" height="100%" border="5px">
        <tr>
            <td>{titles[0]}</td>
        </tr>
        <tr>
            <td>{titles[1]}</td>
        </tr>
        <tr>
            <td>{titles[2]}</td>
        </tr>
        <tr>
            <td>{titles[3]}</td>
        </tr>
        <tr>
            <td>{titles[4]}</td>
        </tr>
    </table>
</body>
</html>"""


with open('temp.html', 'w') as f:
    f.write(html_text)

你输入变量 {} 在文本中,您的样式必须用double转义 {{}} . 试试看。

另外,pythonic的文件写入方式是使用上下文管理器。它不需要 .close() 在打开的文件上。

U10-Forward
Reply   •   2 楼
U10-Forward    6 年前

您在错误的位置结束了字符串,因此请使用下面的完整代码:

titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
html_text = '''<html>
<head>
    <style type="text/css">
        table { border-collapse: collapse;}
        td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }
    </style>
</head>
<body>
    <table width="100%" height="100%" border="5px">
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
    </table>
</body>
</html>''' % (titles[0], titles[1], titles[2], titles[3], titles[4])

f = open('temp.html', 'w')
f.write(html_text)
f.close()

所以现在您将得到预期的html文件。

BoarGules
Reply   •   3 楼
BoarGules    6 年前

格式字符串中有两个错误。正如U9 Forward所指出的,第一个是:

</html> % (titles[0], titles[1], titles[2], titles[3], titles[4])"""

这个 % 是一个插值 操作人员 所以它得走了 之间 字符串和数据:

</html>""" % (titles[0], titles[1], titles[2], titles[3], titles[4])

第二个错误,只有在您修复了那个错误之后,才明显地出现在这里:

<table width="100%" height="100%" border="5px">

当你使用 % 运算符,字符 % 变得特别,所以 %s 做你想做的事。但当这种情况发生时, "100%" 这是不合法的,因为,正如错误消息告诉你的,它把 unsupported format character '"' (0x22) at index 237 . 把光标放在字符串的开头并按右箭头237次,就可以在不到一分钟的时间内找到答案。

在这种情况下, % 你想留下来 % 必须加倍:

<table width="100%%" height="100%%" border="5px">

给出

html_text = '''<html>
<head>
    <style type="text/css">
        table { border-collapse: collapse;}
        td { text-align: center; border: 5px solid #ff0000; border-style: dashed; font-size: 30px; }
    </style>
</head>
<body>
    <table width="100%%" height="100%%" border="5px">
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
        <tr>
            <td>%s</td>
        </tr>
    </table>
</body>
</html>''' % (titles[0], titles[1], titles[2], titles[3], titles[4])

但最根本的问题是python % -字符串是一种格式化迷你语言,而html是一种格式化语言,因此像这样构造html意味着您同时使用两种语言编程。这种双重思考让一些有经验的程序员大吃一惊,但我们其他人更乐于将关注点分离开来,一次只处理一种语言。而不是 % -字符串,考虑使用 lxml 来构造你的html。有更多的学习曲线(由优秀的 tutorial )但是您的代码将更易于编写和维护,并且 LXML 将确保HTML没有语法错误。

buran
Reply   •   4 楼
buran    6 年前

你可以使用一些 template engine that mix logic into template

实例与 jinja2 :

  1. 安装与 pip install jinja2

2那么代码是:

html_text = """<html>
<head>...</head>
<body>
    <table width="100%" height="100%" border="5px">
        {% for title in titles %}
        <tr>
            <td>{{title}}</td>
        </tr>
        {% endfor %}
    </table>
</body>
</html>"""

from jinja2 import Template
titles = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
my_templ = Template(html_text)
with open('temp.html', 'w') as f:
    f.write(my_templ.render(titles=titles))

注意,处理可变长度的列表是灵活的。 模板引擎在web框架中使用。