格式化错误 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'))

我在打印上的错误是:

值错误:转换说明符后的预期":">

事件虽然我已经包括了一个疑问{{}}就像在其他帖子中一样。代码无法通过

感谢您的帮助! 非盟

您希望替换值的位置 {}

要保留文本

{ 或 } 的位置 需要 {{ 或 }}所以对于身体{,你需要身体{{

然后用 }} 关闭它而不是 }。

然后在要替换为 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

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 更干净。

谢谢

既然你标记了python-3.6,那就看看f字符串(PEP 498(。 下面是一个包含一些嵌入式大括号的简单示例。 插入变量的值。 也可以应用格式设置:

>>> 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>

最新更新