相关疑难解决方法(0)

用Python写入UTF-8文件

我真的很困惑codecs.open function.当我做:

file = codecs.open("temp", "w", "utf-8")
file.write(codecs.BOM_UTF8)
file.close()
Run Code Online (Sandbox Code Playgroud)

它给了我错误

UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xef:序号不在范围内(128)

如果我做:

file = open("temp", "w")
file.write(codecs.BOM_UTF8)
file.close()
Run Code Online (Sandbox Code Playgroud)

它工作正常.

问题是为什么第一种方法失败了?我该如何插入bom?

如果第二种方法是正确的做法,那么使用点是codecs.open(filename, "w", "utf-8")什么?

python byte-order-mark utf-8

184
推荐指数
5
解决办法
29万
查看次数

标签 统计

byte-order-mark ×1

python ×1

utf-8 ×1