A simple table that I want to include in an HTML email that sending by Python.
html = """
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;}
th, td {padding: 5px;}
th {text-align: left;}
</style>
</head>
<body>
<h2>Basic HTML Table</h2>
<table style="width:10%">
<tr>
<th>Firstname</th>
</tr>
<tr>
<td>Jill</td>
</tr>
<tr>
<td>Eve</td>
</tr>
</table>
</body>
</html>
"""
It works fine, then I want to insert a variable into, so change it to:
code = "Somebody"
html = """
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;}
th, td {padding: 5px;}
th {text-align: left;}
</style>
</head>
<body>
<h2>Basic HTML Table</h2>
<table style="width:10%">
<tr>
<th>Firstname</th>
</tr>
<tr>
<td>Jill</td>
</tr>
<tr>
<td>{code}</td>
</tr>
</table>
</body>
</html>
""".format(code=code)
It warns:
""".format(code=code)
KeyError: '\n border'
I was thinking to add html = html.readline.rstrip("\n") but the error happens even before that.
How can I have it corrected?
As Tal said you need {} inside {}
For example : {{}}
code = "Somebody"
html = """
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {{
border: 1px solid black;
border-collapse: collapse;
}}
th, td {{padding: 5px;}}
th {{text-align: left;}}
</style>
</head>
<body>
<h2>Basic HTML Table</h2>
<table style="width:10%">
<tr>
<th>Firstname</th>
</tr>
<tr>
<td>Jill</td>
</tr>
<tr>
<td>{code}</td>
</tr>
</table>
</body>
</html>
""".format(code=code)
print(html)