python的csv.reader中文乱码,Python CSV DictReader,带有UTF-8数据

AFAIK, the Python (v2.6) csv module can't handle unicode data by default, correct? In the Python docs there's an example on how to read from a UTF-8 encoded file. But this example only returns the CSV rows as a list.

I'd like to access the row columns by name as it is done by csv.DictReader but with UTF-8 encoded CSV input file.

Can anyone tell me how to do this in an efficient way? I will have to process CSV files in 100's of MByte in size.

解决方案

Actually, I came up with an answer myself (sorry for replying to my own question):

def UnicodeDictReader(utf8_data, **kwargs):

csv_reader = csv.DictReader(utf8_data, **kwargs)

for row in csv_reader:

yield {unicode(key, 'utf-8'):unicode(value, 'utf-8') for key, value in row.iteritems()}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值