代码之家  ›  专栏  ›  技术社区  ›  Andres Urrego Angel

格式错误python 3.6

  •  -1
  • Andres Urrego Angel  · 技术社区  · 6 年前

    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:转换说明符后应为“:”

    尽管我有一个疑问 {{ }} 就像其他职位一样。代码无法通过

    谢谢你的帮助! 澳大利亚

    3 回复  |  直到 6 年前
        1
  •  2
  •   William D. Irons    6 年前

    要替换的值的位置{}

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

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

    然后在每个要替换为值的地方使用{}消息.格式()

    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'))
    
        2
  •  0
  •   Andres Urrego Angel    6 年前

    非常感谢你们的反馈。事实上,我是这么想的;

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

    谢谢

        3
  •  0
  •   Mark Tolonen    6 年前

    自从你贴了标签 python-3.6 ,查看 f-strings ( PEP 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>