【发布时间】:2014-02-15 21:29:32
【问题描述】:
我有一个用 utf-8 编码的 HTML 文件。我想将它输出到一个以 utf-8 编码的文本文件。这是我正在使用的代码:
import codecs
IN = codecs.open("E2P3.html","r",encoding="utf-8")
codehtml = IN.read()
#codehtml = codehtml.decode("utf-8")
texte = re.sub("<br>","\n",codehtml)
#texte = texte.encode("utf-8")
OUT = codecs.open("E2P3.txt","w",encoding="utf-8")
OUT.write(texte)
IN.close()
OUT.close()
如您所见,我已尝试同时使用“解码”和“编解码器”。这些都不起作用,我的输出文本文件默认为 Occidental (Windows-1252) 并且一些实体变得乱码。 我在这里做错了什么?
【问题讨论】:
-
为什么你认为输出文件被编码为Windows-1252?您是否使用了无法检测没有 BOM 的 UTF-8 文件的编辑器?
标签: python encoding utf-8 decode codec