python 3.1.3支持unicode在csv模块? [英] Does python 3.1.3 support unicode in csv module?

查看:559
本文介绍了python 3.1.3支持unicode在csv模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用python 2.6。当我写一个python程序来处理查询结果(csv格式)从sql server。我发现它不支持unicode。



当我使用csv文件运行程序时,出现一个错误:

 对于csvReader中的行:
错误:行包含NULL字节


b $ b

我用Ultraedit保存csv文件为ANSI / ASCII格式后,程序运行正常。



我试图包含编码选项,但是它失败:

  csvReader = csv.reader(open(fname,mode ='rb',encoding ='unicode'这个函数的一个无效关键字参数

csvReader = csv.reader(open(fname,mode ='rb',encoding =' utf-8'),delimiter =',')
TypeError:'encoding'是此函数的无效关键字参数

我想知道python 3是否支持这个unicode阅读。它可以节省我很多工作。

解决方案

Python 3绝对支持unicode。我的猜测是,当您打开CSV文件进行阅读时,您指定了错误(或不?)编码。请参阅: http://docs.python.org/release/3.1 .3 / library / functions.html#open



并尝试类似的操作:

  reader = csv.reader(open(foo.csv,encoding =utf-8))

编辑:如果您使用的是Python 2.6,您可以通过以下方式获得相同的结果:

  import codecs 
reader = csv.reader(codecs.open(foo.csv,encoding =utf-8))



如果你得到空字节,你的文件可能会使用utf-16 ,所以如果文件不能使用utf-8解码,请尝试。


I have been using python 2.6. While I was writing a python program to process the query result ( in csv format ) from sql server. I found it does not support unicode.

When I run the program with csv file, a error poped up saying:

    for row in csvReader:
Error: line contains NULL byte

After I save the csv file in ANSI/ASCII format with Ultraedit, the program is running okay.

I tried to include the encoding option, but it failed:

csvReader = csv.reader(open(fname, mode='rb', encoding='unicode'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function

csvReader = csv.reader(open(fname, mode='rb', encoding='utf-8'), delimiter=',')
TypeError: 'encoding' is an invalid keyword argument for this function

I wonder if python 3 support this unicode reading. It can save me a lot of work.

解决方案

Python 3 definitely supports unicode. My guess is that you specified the wrong (or no?) encoding when you opened the CSV file for reading. See: http://docs.python.org/release/3.1.3/library/functions.html#open

And try something like:

reader = csv.reader(open("foo.csv", encoding="utf-8"))

Edit: If you are using Python 2.6, you can achieve the same result with:

import codecs
reader = csv.reader(codecs.open("foo.csv", encoding="utf-8"))

HOWEVER if you're getting null bytes, your file may be encoded using "utf-16", so try that if the file can't be decoded using utf-8.

这篇关于python 3.1.3支持unicode在csv模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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