博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
utf 8 - Write to utf-8 file in python - Stack Overflow
阅读量:6830 次
发布时间:2019-06-26

本文共 778 字,大约阅读时间需要 2 分钟。

Disclaimer: I'm not a Python programmer.

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!"

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 codecsfile = codecs.open("lol", "w", "utf-8")file.write(u'\ufeff')file.close()

(That seems to give the right answer - a file with bytes EF BB BF.)

EDIT: 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.

转载地址:http://gztkl.baihongyu.com/

你可能感兴趣的文章
一个简单的统计图像主颜色的算法(C#源代码)
查看>>
java开发中的重中之重-------mysql(基础篇)
查看>>
While 나가는 법
查看>>
c语言操作符的优先级
查看>>
Codeforces Round #420 (Div. 2) A-E
查看>>
9.4-9.19 h5日记总结
查看>>
mysql支持的存储引擎
查看>>
input checkbox复选框取值
查看>>
SimpleAdapter
查看>>
一 python并发编程之多进程
查看>>
Socket通信
查看>>
less文件编译成微信小程序wxss文件
查看>>
viewport 学习
查看>>
jackson详解
查看>>
Python-JS事件与面向对象操作
查看>>
Linux启动过程总结
查看>>
nexus搭建maven私服
查看>>
新建android工程的时候eclipse没有生成MainActivity和layout布局
查看>>
Halcon算子翻译——dev_get_system
查看>>
div模拟textarea文本域轻松实现高度自适应
查看>>