UTF8 Python BOM表 [英] UTF8 Python BOM

查看:268
本文介绍了UTF8 Python BOM表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
在python中写入utf-8文件

Possible Duplicate:
Write to utf-8 file in python

我有要写入CSV文件的Unicode字符串(带有日语字符). 但是,BOM似乎没有正确编写,就像第一行中的字符串""一样.这导致Excel无法正确显示日语字符.使用Notepad ++打开CSV时,字符会正确显示.

I have Unicode strings (with Japanese characters) I want to write to a CSV file. However, the BOM does not seem to be written correctly, just as a string "" in the first line. This leads to Excel not displaying the Japanese characters correctly. When opening the CSV with Notepad++, the characters are displayed correctly.

fileObj = codecs.open(filename,"w",'utf-8')
fileObj.write(codecs.BOM_UTF8)
c = u';'
for s in stringsToWrite:
   line = e.someUnicodeString
   fileObj.write(line)
fileObj.close()

推荐答案

fileObj = codecs.open(filename,"w",'utf-8')

好的,您有Unicode输出流.

OK, you have a Unicode output stream.

fileObj.write(codecs.BOM_UTF8)

BOM_UTF8是一个字节序列,而不是您期望写入Unicode流的Unicode字符串. Python会使用某些可能不正确的编码将字节自动转换为Unicode.如果默认编码是Windows代码页1252而不是UTF-8,则您将有效地对BOM进行双重编码,并且将其作为的UTF-8编码.

BOM_UTF8 is a sequence of bytes, not a Unicode string as you would expect to write to a Unicode stream. Python will automatically convert from bytes to Unicode using some encoding which may not be the correct one. If the default encoding is Windows code page 1252 rather than UTF-8, you'll be effectively double-encoding the BOM and it will come as the UTF-8 encoding of .

建议将BOM作为Unicode字符编写:

Suggest writing the BOM as the Unicode character it is instead:

fileObj.write(u'\uFEFF')

InternetSeriousBusiness写道:

InternetSeriousBusiness wrote:

不是不鼓励UTF-8 BOM吗? –

Isn't the UTF-8 BOM discouraged, anyway? –

是的,在大多数情况下,UTF-8仿造的BOM在很大程度上是一场灾难,但是需要Excel的字符集来猜测要使用UTF-8,这是必需的.不幸的是,它不适用于Mac版Excel.另一种可能的方法是使用UTF-16.

Yes, the UTF-8 faux-BOM is largely a disaster in most contexts, but it is needed to get Excel's charset guessing to pick up UTF-8. Unfortunately it doesn't work in Excel for Mac. Another possible approach might be to use UTF-16.

这篇关于UTF8 Python BOM表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆