第一種:CSV寫入中文python
#! /usr/bin/env python # _*_ coding:utf-8 _*_ import csv csvfile = file('test.csv', 'wb') csvfile.write(u'\ufeff'.encode('utf8')) writer = csv.writer(csvfile) writer.writerow(['id', 'url', 'keywords']) data = [ ('1', 'http://www.baidu.com/', '百度'), ('2', 'http://www.taobao.com/', '淘寶'), ('3', 'http://www.jd.com/', '京東') ] writer.writerows(data) csvfile.close()
運行截圖:url
第二種:spa
利用unicodecsv 將中文寫入csv文件:.net
#!/usr/bin/python # -*- coding:utf-8 -*- import unicodecsv as ucsv data = [[u"列1", u"列2"], [u"內容1", u"內容2"]] with open('test.csv', 'wb') as f: w = ucsv.writer(f, encoding = 'gbk') w.writerows(data)
運行後,用excel打開的效果:3d
參考連接:https://www.jb51.net/article/132732.htmexcel