python操作csv文件范例

简介

使用python正确操作csv文件,防止乱码

代码

import codecs
import csv

csv_filename = '/tmp/hoge.csv'

with open(csv_filename, 'wb') as f:
    f.write(codecs.BOM_UTF16_LE)

with codecs.open(csv_filename, 'ab', encoding = 'utf-16-le', errors = 'replace') as f:
    tsv = csv.writer(f, delimiter='\t')
    tsv.writerows(
        [
            ['aa', 'bb', 'cc'],
            ['xx', 'yy', 'zz'],
            ['这是', '测试', '中文', '行']
        ]
    )