代码之家  ›  专栏  ›  技术社区  ›  Hoseong Jeon

用python格式化html

  •  0
  • Hoseong Jeon  · 技术社区  · 6 年前

    我想用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]。

    我怎样才能做到呢?

    4 回复  |  直到 6 年前
        1
  •  1
  •   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框架中使用。

        2
  •  1
  •   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没有语法错误。

        3
  •  0
  •   U13-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文件。

        4
  •  0
  •   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() 在打开的文件上。