格式化错误python 3.6

时间:2018-08-29 21:08:02

标签: python string python-3.6

我收到一条HTML消息,该消息已映射为字符串,然后我想用一个参数替换3个变量,但是我为此感到吃力。这是代码:

message = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<style>
    body {
    !background-color: #ffffcc;
    font-family:courier;
    font-size: 120%
}
</style>
<div>
    <h2 style="text-decoration:underline">Alert process failure</h2>
    <p> The process <b>{{ }}</b> has failure in the method <b>{{ }}</b> due the error <b>{{ }}</b>. Please take a look the system log and take the required 
        actions in order to solve the problem ASAP and communicate the end users.</p>

</div>
</body>
</html>
"""

print(message.format('1','2','2'))

我在打印上的错误是:

  

ValueError:转换说明符后应为':'

尽管我像其他帖子一样包含了一个{{}}的疑问,但仍是事件。代码无法通过

感谢您的帮助! AU

3 个答案:

答案 0 :(得分:2)

在需要替换值的地方{}

要保留文字{或}的地方{{或}}

所以对于身体{,您需要身体{{

,然后使用}}(而不是})将其关闭。

然后在每个要替换为message.format()中的值的地方使用{}

message = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<style>
    body {{
    !background-color: #ffffcc;
    font-family:courier;
    font-size: 120%
}}
</style>
<div>
    <h2 style="text-decoration:underline">Alert process failure</h2>
    <p> The process <b>{}</b> has failure in the method <b>{}</b> due the error <br>{}</b>. Please take a look the system log and take the required
        actions in order to solve the problem ASAP and communicate the end users.</p>

</div>
</body>
</html>
"""

print(message.format('1','2','2'))

答案 1 :(得分:0)

非常感谢您的反馈。记录下来,这就是我的想法;

选项1

from string import Template

message = Template("""
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<style>
    body {
    !background-color: #ffffcc;
    font-family:courier;
    font-size: 120%
}
</style>
<div>
    <h2 style="text-decoration:underline">BI Alert process failure</h2>
    <p> The process <b> $fname </b> has failure in the method <b> $method </b> due the error <b> $error </b>. Please take a look the system log and take the required 
        actions in order to solve the problem ASAP and communicate the end users.</p>

</div>
<div style="text-decoration:underline">
    <p> For more information contact to <b>BI</b> team.</p>
    <img src="https://s3.amazonaws.com/gp-process/etl-deployment/chronos/medias/logo.png"
    width="100" height="100">
</div>
</body>
</html>
""")

print(message.substitute(fname = 'hello',method='two',error='hi'))

**选项2 **

message = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<style>
    body {{
    !background-color: #ffffcc;
    font-family:courier;
    font-size: 120%
}}
</style>
<div>
    <h2 style="text-decoration:underline">BI Alert process failure</h2>
    <p> The process <b> $fname </b> has failure in the method <b> $method </b> due the error <b> $error </b>. Please take a look the system log and take the required 
        actions in order to solve the problem ASAP and communicate the end users.</p>

</div>
<div style="text-decoration:underline">
    <p> For more information contact to <b>BI</b> team.</p>
    <img src="https://s3.amazonaws.com/gp-process/etl-deployment/chronos/medias/logo.png"
    width="100" height="100">
</div>
</body>
</html>
"""

print(message.format('1','2','3'))

顺便说一句,我认为选项1更干净。

谢谢

答案 2 :(得分:0)

由于您标记了python-3.6,因此请查看f-stringsPEP 498)。这是一个带有一些嵌入式花括号的简单示例。插入变量的值。 Formatting也可以应用:

>>> a,b,c = 1,2,3
>>> print(f'{{a={a} b={b} c={c:02}}}')
{a=1 b=2 c=03}

您的解决方案:

def do_error(fname,message,error):
    print(f'''
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>
    <style>
        body {{
        !background-color: #ffffcc;
        font-family:courier;
        font-size: 120%
    }}
    </style>
    <div>
        <h2 style="text-decoration:underline">Alert process failure</h2>
        <p> The process <b>{fname}</b> has failure in the method <b>{message}</b> due the error <b>{error}</b>. Please take a look the system log and take the required 
            actions in order to solve the problem ASAP and communicate the end users.</p>
    </div>
    </body>
    </html>
    ''')

do_error('FNAME','MESSAGE','ERROR')

输出:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<style>
    body {
    !background-color: #ffffcc;
    font-family:courier;
    font-size: 120%
}
</style>
<div>
    <h2 style="text-decoration:underline">Alert process failure</h2>
    <p> The process <b>FNAME</b> has failure in the method <b>MESSAGE</b> due the error <b>ERROR</b>. Please take a look the system log and take the required 
        actions in order to solve the problem ASAP and communicate the end users.</p>
</div>
</body>
</html>