CSV,DictWriter,unicode和utf-8

时间:2010-07-19 22:24:54

标签: python unicode csv utf-8

我遇到了DictWriter和非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()

给出这个回溯:

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)

我有点迷失,因为DictWriter应该能够使用我在文档中读到的UTF-8。

1 个答案:

答案 0 :(得分:9)

您使用codecs.open获得的对象需要在其write方法中使用 unicode 字符串 - 这就是重点。 csv.DictWriter当然是使用utf8编码的字节串来调用该方法,而不是异常。

f的创建更改为f = open("test.csv", 'wb')(从图片中取出codecs),事情应该正常。