-1

I get a html message that I have mapped as string and then I want to replace 3 variables with a parameter but I'm struggling with this. here is the code:

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

My error on the print is:

ValueError: expected ':' after conversion specifier

Event though I have included a doubt {{ and }} like in other posts. The code is not going thru

Thanks for the help! AU

Andres Urrego Angel
  • 1,842
  • 7
  • 29
  • 55
  • 1
    In addition to the `{}` in your string, you also have things like that `body { … }`. So, it's going to try to interpret the whole thing inside those braces as a format spec, and it isn't a valid format spec (but it's actually kind of close to one, which is why the error is a bit confusing…), so it fails. – abarnert Aug 29 '18 at 21:13
  • 2
    If you want literal braces in your format string, you have to escape them by doubling them, like `body {{ … }}`. – abarnert Aug 29 '18 at 21:13
  • This is almost certainly a duplicate of a question with a great answer that explains it better than I can; if I can't find it, hopefully someone else can. – abarnert Aug 29 '18 at 21:14
  • Possible duplicate of [How can I print literal curly-brace characters in python string and also use .format on it?](https://stackoverflow.com/questions/5466451/how-can-i-print-literal-curly-brace-characters-in-python-string-and-also-use-fo) – ostrokach Aug 29 '18 at 21:15
  • I have done the double `{{` and `}}` and it's not going thru :( – Andres Urrego Angel Aug 29 '18 at 21:25
  • @AndresUrregoAngel You need the `{{` where you want a literal `{`. It doesn't look like that's what you have. – Stop harming Monica Aug 29 '18 at 21:30

3 Answers3

2

Where you want the values replaced you want {}

Where you want to preserve the literal { or } you want {{ or }}

so for body {, you need body {{

and then close it with }} instead of }.

Then use {} in each place you want to substitute with a value in 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'))
William D. Irons
  • 2,244
  • 1
  • 18
  • 19
0

Guys thanks so much for your feedback. For the record this is how I figure it out;

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

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

By the way, I think option 1 is cleaner.

thanks

Andres Urrego Angel
  • 1,842
  • 7
  • 29
  • 55
0

Since you tagged python-3.6, look into f-strings (PEP 498). Here's a simple example with some embedded curly braces. Values of variables are inserted. Formatting can be applied as well:

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

Your solution:

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

Output:

<!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>
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251