參考資料:https://docs.python.org/2/library/csv.html
生成csv文件
import csv with open('egg.csv', 'wb') as csvfile: spamwriter = csv.writer(csvfile,dialect='excel') spamwriter.writerow(['a', '1', '1', '2', '2']) spamwriter.writerow(['b', '3', '3', '6', '4']) spamwriter.writerow(['c', '7', '7', '10', '4']) spamwriter.writerow(['d', '11','11','11', '1']) spamwriter.writerow(['e', '12','12','14', '3'])
可以用excel打開csv文件
提示:使用python的csv生成excel所兼容的csv文件的話,主要就是創建writer時的參數時要有dialect=’excel’,就可以了。
用python讀取csv文件:
import csv with open('egg.csv') as csvfile: reader = csv.DictReader(csvfile) for row in reader: print row