代码:
# 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()
- wb中的w表示写入模式,b是文件模式
- 写入一行用writerow
- 多行用writerows
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()
运行结果:
root@he-desktop:~/python/example# python read_csv.py
['xe5xa7x93xe5x90x8d', 'xe5xb9xb4xe9xbex84', 'xe7x94xb5xe8xafx9d']
['xe5xb0x8fxe6xb2xb3', '25', '1234567']
['xe5xb0x8fxe8x8axb3', '18', '789456']