在 Python 中写入 UTF-8 文件 [英] Write to UTF-8 file in Python
问题描述
我真的对 codecs.open 函数
感到困惑.当我这样做时:
I'm really confused with the codecs.open function
. When I do:
file = codecs.open("temp", "w", "utf-8")
file.write(codecs.BOM_UTF8)
file.close()
它给了我错误
UnicodeDecodeError: 'ascii' 编解码器无法解码字节 0xef 的位置0:序数不在范围内(128)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
如果我这样做:
file = open("temp", "w")
file.write(codecs.BOM_UTF8)
file.close()
效果很好.
问题是为什么第一种方法会失败?以及如何插入 bom?
Question is why does the first method fail? And how do I insert the bom?
如果第二种方法是正确的方法,那么使用 codecs.open(filename, "w", "utf-8")
有什么意义?
If the second method is the correct way of doing it, what the point of using codecs.open(filename, "w", "utf-8")
?
推荐答案
我认为问题在于 codecs.BOM_UTF8
是一个字节字符串,而不是一个 Unicode 字符串.我怀疑文件处理程序试图根据我打算将 Unicode 编写为 UTF-8 编码文本,但您给了我一个字节字符串!"来猜测您的真正意思.
I believe the problem is that codecs.BOM_UTF8
is a byte string, not a Unicode string. I suspect the file handler is trying to guess what you really mean based on "I'm meant to be writing Unicode as UTF-8-encoded text, but you've given me a byte string!"
尝试直接编写字节顺序标记的 Unicode 字符串(即 Unicode U+FEFF),以便文件将其编码为 UTF-8:
Try writing the Unicode string for the byte order mark (i.e. Unicode U+FEFF) directly, so that the file just encodes that as UTF-8:
import codecs
file = codecs.open("lol", "w", "utf-8")
file.write(u'ufeff')
file.close()
(这似乎给出了正确的答案 - 一个包含字节 EF BB BF 的文件.)
(That seems to give the right answer - a file with bytes EF BB BF.)
S. Lott 的建议使用utf-8-sig"作为编码比自己明确地编写 BOM,但我会在这里留下这个答案,因为它解释了之前出了什么问题.
S. Lott's suggestion of using "utf-8-sig" as the encoding is a better one than explicitly writing the BOM yourself, but I'll leave this answer here as it explains what was going wrong before.
这篇关于在 Python 中写入 UTF-8 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!