python open write的编码_Python DictWriter编写UTF-8编码的CSV文件

I have a list of dictionaries containing unicode strings.

csv.DictWriter can write a list of dictionaries into a CSV file.

I want the CSV file to be encoded in UTF8.

The csv module cannot handle converting unicode strings into UTF8.

The csv module documentation has an example for converting everything to UTF8:

:

def utf_8_encoder(unicode_csv_data):

for line in unicode_csv_data:

yield line.encode('utf-8')

It also has a class UnicodeWriter:.

But... how do I make DictWriter work with these? Wouldn't they have to inject themselves in the middle of it, to catch the disassembled dictionaries and encode them before it writes them to the file? I don't get it.

解决方案

If using Python 2.7 or later, use a dict comprehension to remap the dictionary to utf-8 before passing to DictWriter:

# coding: utf-8

import csv

D = {'name':u'马克','pinyin':u'mǎkè'}

f = open('out.csv','wb')

f.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)

w = csv.DictWriter(f,sorted(D.keys()))

w.writeheader()

w.writerow({k:v.encode('utf8') for k,v in D.items()})

f.close()

You can use this idea to update UnicodeWriter to DictUnicodeWriter:

# coding: utf-8

import csv

import cStringIO

import codecs

class DictUnicodeWriter(object):

def __init__(self, f, fieldnames, dialect=csv.excel, encoding="utf-8", **kwds):

# Redirect output to a queue

self.queue = cStringIO.StringIO()

self.writer = csv.DictWriter(self.queue, fieldnames, dialect=dialect, **kwds)

self.stream = f

self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, D):

self.writer.writerow({k:v.encode("utf-8") for k,v in D.items()})

# Fetch UTF-8 output from the queue ...

data = self.queue.getvalue()

data = data.decode("utf-8")

# ... and reencode it into the target encoding

data = self.encoder.encode(data)

# write to the target stream

self.stream.write(data)

# empty queue

self.queue.truncate(0)

def writerows(self, rows):

for D in rows:

self.writerow(D)

def writeheader(self):

self.writer.writeheader()

D1 = {'name':u'马克','pinyin':u'Mǎkè'}

D2 = {'name':u'美国','pinyin':u'Měiguó'}

f = open('out.csv','wb')

f.write(u'\ufeff'.encode('utf8')) # BOM (optional...Excel needs it to open UTF-8 file properly)

w = DictUnicodeWriter(f,sorted(D.keys()))

w.writeheader()

w.writerows([D1,D2])

f.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值