CSV, DictWriter, unicode和utf-8

时间:2023-01-06 11:49:02

I am having problems with the DictWriter and non-ascii characters. A short version of my problem:

我对双字符和非ascii字符有问题。我的问题的一个简短版本:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import csv

f = codecs.open("test.csv", 'w', 'utf-8')
writer = csv.DictWriter(f, ['field1'], delimiter='\t')
writer.writerow({'field1':u'å'.encode('utf-8')})
f.close()

Gives this Traceback:

给这个回溯:

Traceback (most recent call last):
File "test.py", line 10, in <module>writer.writerow({'field1':u'å'.encode('utf-8')})
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/csv.py", line 124, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 638, in write
return self.writer.write(data)
File "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/codecs.py", line 303, in write data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

I am bit lost as the DictWriter ought to be able to work with UTF-8 from what I have read in the documentation.

我有点迷惑,因为DictWriter应该能够使用UTF-8,从我在文档中看到的内容来看。

1 个解决方案

#1


9  

The object you obtain with codecs.open wants a unicode string in its write method -- that's the whole point. csv.DictWriter of course is calling that method with a utf8-encoded byte string instead, whence the exception.

用编解码器获得的对象。open的写方法中需要一个unicode字符串——这就是关键所在。csv。DictWriter当然是用一个utf8编码的字节字符串来调用这个方法,从中产生异常。

Change f's creation to f = open("test.csv", 'wb') (taking codecs out of the picture) and things should work just fine.

将f的创建更改为f = open(“test”)。csv“,‘wb’)(从图片中去掉了codecs),事情就可以正常工作了。

#1


9  

The object you obtain with codecs.open wants a unicode string in its write method -- that's the whole point. csv.DictWriter of course is calling that method with a utf8-encoded byte string instead, whence the exception.

用编解码器获得的对象。open的写方法中需要一个unicode字符串——这就是关键所在。csv。DictWriter当然是用一个utf8编码的字节字符串来调用这个方法,从中产生异常。

Change f's creation to f = open("test.csv", 'wb') (taking codecs out of the picture) and things should work just fine.

将f的创建更改为f = open(“test”)。csv“,‘wb’)(从图片中去掉了codecs),事情就可以正常工作了。