首页 > 代码库 > Python读写csv文件

Python读写csv文件

1. 写csv文件

# coding: utf-8import csvcsvfile = 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-8import csvcsvfile = file(csv_test.csv, rb)reader = csv.reader(csvfile)for line in reader:    print linecsvfile.close()