首页 > 解决方案 > 如何在html中将字符串编码为utf8

问题描述

我正在尝试使用 awdeoroi 邮件合并。在 html 模板中,我在段落标签中有法语编码的字符。当我执行邮件我有编码错误:

UnicodeDecodeError : 'utf-8' codec can't decode byte 0xf4 in position 81: invalid continuation byte

如何对这些段落进行编码,以便它们在 python 中得到很好的处理?

TO: {{email}}
SUBJECT: Testing mailmerge
FROM: My Self <myself@mydomain.com>
Content-Type: text/html

<html>
<body>

<p>Hi, {{name}},</p>

<p>Your number is {{number}}.</p>

<p>Sent by <a href="https://github.com/awdeorio/mailmerge">Here is the paragraph. Ce texte est en francais. <b>Accentué<b>. L'ideal</a></p>

</body>
</html>

标签: pythonhtmlpython-3.xcharacter-encodingmailmerge

解决方案


十六进制f4是 latin1 的ô. 如果这是在 Python 中输入的,则需要在源文件的开头:

# -*- coding: utf-8 -*-

如果数据来自数据库,请提供更多详细信息。

如果文本来自其他地方,请提供更多详细信息。


推荐阅读