【问题标题】:how to encode string to utf8 inside html如何在html中将字符串编码为utf8
【发布时间】:2020-11-25 22:04:22
【问题描述】:

我正在尝试使用 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>

【问题讨论】:

  • 您的“邮件”似乎不是 UTF-8。发现它是什么编码,然后相应地威胁它。但在电子邮件中,最好使用 html 实体,而不是直接字符。
  • 检测字节串的字符编码,看看chardet

标签: python html python-3.x character-encoding mailmerge


【解决方案1】:

十六进制 f4ô 的 latin1。如果这是在 Python 中输入的,您需要在源文件的开头使用它:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2010-12-02
    • 2017-04-14
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多