python - 如何在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>
解决方案
十六进制f4
是 latin1 的ô
. 如果这是在 Python 中输入的,则需要在源文件的开头:
# -*- coding: utf-8 -*-
如果数据来自数据库,请提供更多详细信息。
如果文本来自其他地方,请提供更多详细信息。
推荐阅读
- git - 从本地推送到远程时的空文件夹
- laravel - Data from geoJSON API call in Larvel 5.8
- c# - C# 接口和泛型协同工作
- arrays - Efficiently read CSV file into array of doubles in C
- pandas - Pandas read_hdf5 partial matching of a string
- assembly - 如何让 BSR 指令在 64 位上工作?
- unix - 使用 awk 比较来自两个不同文件的两个字段
- javascript - 具有多个过滤器的 Javascript 过滤器数组
- vbscript - 使用 vbscript 仅删除文本文件的第一行
- c# - 关闭返回值集中的计数