ValueError: unsupported format character ';' (0x3b) at index 946 HTML email with pandas dataframe
ValueError: unsupported format character ';' (0x3b) at index 946 HTML email with pandas dataframe
我正在尝试使用 pandas 数据帧
以 HTML 格式发送电子邮件
我已经使用 .to_html(index = False)
将 pandas 数据帧转换为 html 代码
使用 %s
将转换后的数据帧传递给 HTML 代码时抛出错误,因为
ValueError: unsupported format character ';' (0x3b) at index 894
当我使用 .format()
传递它抛出的参数时
KeyError: '\npadding' error
这是我的代码
html_body = '''<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<style>
th,td{
padding: 10px;
}
.dataframe{
border:1px;
width: 80%;
padding: 25px;
margin: auto;
margin-top:30px;
border: 1px solid black;
}
tr:nth-child(even) {
background-color: #dee2e6;
border: 1px solid black;
}
table thead tr {
background-color: #dee2e6;
}
td{
background-color: white;
}
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<h2 style="text-align: center;"> We found anomalies in following record/s </h2>
{dataframe}
</body>
</html>'''.format(dataframe=html_df)
我认为 ValueError
是因为您的 html 文本 (width = 80%;
) 中有一个未转义的 %
字符。如果用双 %%
替换它,它应该可以工作 (width: 80%%;
),如下例所示:
html_body = '''<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<style>
th,td{
padding: 10px;
}
.dataframe{
border:1px;
width: 80%%;
padding: 25px;
margin: auto;
margin-top:30px;
border: 1px solid black;
}
tr:nth-child(even) {
background-color: #dee2e6;
border: 1px solid black;
}
table thead tr {
background-color: #dee2e6;
}
td{
background-color: white;
}
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<h2 style="text-align: center;"> We found anomalies in following record/s </h2>
%(dataframe)s
</body>
</html>''' % {"dataframe":"bla"}
我正在尝试使用 pandas 数据帧
以 HTML 格式发送电子邮件我已经使用 .to_html(index = False)
使用 %s
将转换后的数据帧传递给 HTML 代码时抛出错误,因为
ValueError: unsupported format character ';' (0x3b) at index 894
当我使用 .format()
传递它抛出的参数时
KeyError: '\npadding' error
这是我的代码
html_body = '''<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<style>
th,td{
padding: 10px;
}
.dataframe{
border:1px;
width: 80%;
padding: 25px;
margin: auto;
margin-top:30px;
border: 1px solid black;
}
tr:nth-child(even) {
background-color: #dee2e6;
border: 1px solid black;
}
table thead tr {
background-color: #dee2e6;
}
td{
background-color: white;
}
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<h2 style="text-align: center;"> We found anomalies in following record/s </h2>
{dataframe}
</body>
</html>'''.format(dataframe=html_df)
我认为 ValueError
是因为您的 html 文本 (width = 80%;
) 中有一个未转义的 %
字符。如果用双 %%
替换它,它应该可以工作 (width: 80%%;
),如下例所示:
html_body = '''<!DOCTYPE html>
<html>
<head>
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<!-- JS, Popper.js, and jQuery -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<style>
th,td{
padding: 10px;
}
.dataframe{
border:1px;
width: 80%%;
padding: 25px;
margin: auto;
margin-top:30px;
border: 1px solid black;
}
tr:nth-child(even) {
background-color: #dee2e6;
border: 1px solid black;
}
table thead tr {
background-color: #dee2e6;
}
td{
background-color: white;
}
table, th, td{
border: 1px solid black;
}
</style>
</head>
<body>
<h2 style="text-align: center;"> We found anomalies in following record/s </h2>
%(dataframe)s
</body>
</html>''' % {"dataframe":"bla"}