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

Python168的学习笔记5

时间:2016-11-12 01:30:10      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:open   标准库   row   head   退出   笔记   headers   john   ext   

关于对csv文件的操作。

python标准库中有csv的库,使用非常方便。

 1 import csv
 2 
 3 with open(pingan.csv,rb) as rf:
 4     reader = csv.reader(rf)#读操作
 5     with open(pingan2.csv,wb) as wf:
 6         writer = csv.writer(wf)
 7         headers = reader.next()
 8         writer.writerow(headers)#写操作
 9         for row in reader:
10             if row[0] < 2016-01-01:#满足条件就退出循环
11                 break            
12             if int(row[5]) >= 50000000:#对特定数据的判断
13                 writer.writerow(row)

 

关于对json文件的操作。

python标准库中也有json的库,操作也很方便。

 1 import json
 2 
 3 l = (1,2,abc,{name:john,age:18})
 4 
 5 with open(demo.json,wb) as f:
 6     json.dump(l,f)
 7     
 8 with open(demo.json,rb) as f:
 9     r = json.load(f)  

 

Python168的学习笔记5

标签:open   标准库   row   head   退出   笔记   headers   john   ext   

原文地址:http://www.cnblogs.com/nanrou/p/6055887.html

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