1. 写csv文件
# coding: utf-8 import csv csvfile = file(‘csv_test.csv‘, ‘wb‘) writer = csv.writer(csvfile) writer.writerow([‘姓名‘, ‘年龄‘, ‘电话‘]) #写入单行 data = [ (‘小河‘, ‘25‘, ‘1234567‘), (‘小芳‘, ‘18‘, ‘789456‘) ] writer.writerows(data) #写入多行 csvfile.close()
2.读csv文件
# coding: utf-8 import csv csvfile = file(‘csv_test.csv‘, ‘rb‘) reader = csv.reader(csvfile) for line in reader: print line csvfile.close()