Python unicode字符串中的unicode DECSV读取器不工作?

Python unicode字符串中的unicode DECSV读取器不工作?,python,csv,unicode,Python,Csv,Unicode,我在将unicode CSV字符串读入python unicode DESCV时遇到问题: >>> import unicodecsv, StringIO >>> f = StringIO.StringIO(u'é,é') >>> r = unicodecsv.reader(f, encoding='utf-8') >>> row = r.next() Traceback (most recent call last):

我在将unicode CSV字符串读入python unicode DESCV时遇到问题:

>>> import unicodecsv, StringIO
>>> f = StringIO.StringIO(u'é,é')
>>> r = unicodecsv.reader(f, encoding='utf-8')
>>> row = r.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/guy/test/.env/lib/python2.7/site-packages/unicodecsv/__init__.py", line 101, in next
    row = self.reader.next()
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
使用cStringIO尝试我的代码失败,因为cStringIO不能接受unicode(所以我不知道为什么这个示例有效!)

>>从cStringIO导入StringIO
>>>f=StringIO(u'é')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
UnicodeEncodeError:“ascii”编解码器无法对位置0中的字符u'\xe9'进行编码:序号不在范围内(128)
我需要从web textarea表单字段接受UTF-8 CSV格式的输入,因此不能只是从文件中读取


有什么想法吗?

unicodesv文件为您读取和解码字节字符串。您正在传递它
unicode
字符串。在输出时,使用配置的编解码器将unicode值编码为ByTestRing

此外,
cStringIO.StringIO
只能处理编码的bytestrings,而纯python
StringIO.StringIO
类愉快地将
unicode
值视为字节字符串

解决方案是在将unicode值放入
StringIO
对象之前对其进行编码:

>>> import unicodecsv, StringIO, cStringIO
>>> f = StringIO.StringIO(u'é,é'.encode('utf8'))
>>> r = unicodecsv.reader(f, encoding='utf-8')
>>> next(r)
[u'\xe9', u'\xe9']
>>> f = cStringIO.StringIO(u'é,é'.encode('utf8'))
>>> r = unicodecsv.reader(f, encoding='utf-8')
>>> next(r)
[u'\xe9', u'\xe9']

杰出的回答得好,速度快。一定要这样爱:)
>>> from cStringIO import StringIO
>>> f = StringIO(u'é')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
>>> import unicodecsv, StringIO, cStringIO
>>> f = StringIO.StringIO(u'é,é'.encode('utf8'))
>>> r = unicodecsv.reader(f, encoding='utf-8')
>>> next(r)
[u'\xe9', u'\xe9']
>>> f = cStringIO.StringIO(u'é,é'.encode('utf8'))
>>> r = unicodecsv.reader(f, encoding='utf-8')
>>> next(r)
[u'\xe9', u'\xe9']