用Python写入UTF-8文件 [英] Write to UTF-8 file in Python
问题描述
我真的对codecs.open function
感到困惑.当我这样做时:
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()
它工作正常.
问题是为什么第一种方法失败?以及如何插入物料清单?
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 U + FEFF)编写Unicode字符串,以便文件将其编码为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屋!