在csv中写入unicode数据 [英] Writing unicode data in csv

查看:179
本文介绍了在csv中写入unicode数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似的问题已经被问过很多次,但我认为我没有能够正确实现csv作者在csv中正确写入(它显示垃圾)。



我想使用UnicodeWriter,如官方文档中所述。

  ff = open('a.csv','w')
writer = UnicodeWriter(ff)
st = unicode('Displaygrößen','utf-8')#gives(u'Displaygr\xf6\xdfen','utf-8')
writer.writerow([st])

这不会给我任何解码或编码错误。但它写的字Displaygrößen作为Displaygrößen这是不好的。

解决方案

您正在以UTF-8格式编写一个文件,但是,您不要将其指定到您的csv文件中。



您应该在文件开头处写入UTF-8头。添加:

  ff = open('a.csv','w')
ff.write .BOM_UTF8)

然后你的csv文件应该打开正确后,程序试图读取它。 / p>

I know similar kind of question has been asked many times but seriously i have not been able to properly implement the csv writer which writes properly in csv (it shows garbage).

I am trying to use UnicodeWriter as mention in official docs .

ff = open('a.csv', 'w')
writer = UnicodeWriter(ff)
st = unicode('Displaygrößen', 'utf-8') #gives (u'Displaygr\xf6\xdfen', 'utf-8')
writer.writerow([st])

This does not give me any decoding or encoding error. But it writes the word Displaygrößen as Displaygrößen which is not good. Can any one help me what i am doing wrong here??

解决方案

You are writing a file in UTF-8 format, but you don't indicate that into your csv file.

You should write the UTF-8 header at the beginning of the file. Add this:

ff = open('a.csv', 'w')
ff.write(codecs.BOM_UTF8)

And your csv file should open correctly after that with the program trying to read it.

这篇关于在csv中写入unicode数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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