码迷,mamicode.com
首页 > 编程语言 > 详细

Python读写csv文件

时间:2014-07-16 22:57:55      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   文件   for   

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() 

Python读写csv文件,布布扣,bubuko.com

Python读写csv文件

标签:style   blog   color   os   文件   for   

原文地址:http://www.cnblogs.com/yu-zhang/p/3835311.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!