参考资料: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
时间: 2024-10-12 07:43:10