Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83783
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 250
  • 用 户 组: 普通用户
  • 注册时间: 2015-08-12 22:31
文章分类

全部博文(26)

文章存档

2016年(26)

我的朋友

分类: Python/Ruby

2016-12-13 12:30:27

一,json


1.    内存中操作

  1. import json
  2. dic = {"k1": 2, "k2": 2}
  3. res1 = json.dumps(dic)
  4. #把python的基本数据类型转化成字符串

  5. print(res1, type(res1))

  6. res2 = json.loads(res1)
  7. #把字符串转化成python的基本数据类型,但是该字符串
  8. #要符合python基本数据类型的特点才能转化成功。

  9. print(res2, type(res2))


  10. #注意通过loads进行反序列化的时候,一定要使用双引号,因为其他语言
  11. 中双引号和单引号表示的不同,为了便于跨平台的操作,因此必须统一。

  12. ----------------------------------------------------------------
  13. 举例:
  14. import requests
  15. import json
  16. response = requests.get("北京")
  17. response.encoding = "utf-8"
  18. dic = json.loads(response.text)
  19. print(dic, type(dic))

  20. 结果:
  21. {'data': {'ganmao': '昼夜温差较大,较易发生感冒,请适当增减衣服。体质较弱的朋友请注意防护。', 'forecast': [{'low': '低温 -2℃', 'fengli': '微风级', 'fengxiang': '北风', 'type': '霾', 'date': '12日星期一', 'high': '高温 6℃'}, {'low': '低温 -7℃', 'fengli': '微风级', 'fengxiang': '无持续风向', 'type': '多云', 'date': '13日星期二', 'high': '高温 3℃'}, {'low': '低温 -5℃', 'fengli': '微风级', 'fengxiang': '无持续风向', 'type': '晴', 'date': '14日星期三', 'high': '高温 2℃'}, {'low': '低温 -5℃', 'fengli': '3-4级', 'fengxiang': '北风', 'type': '晴', 'date': '15日星期四', 'high': '高温 6℃'}, {'low': '低温 -5℃', 'fengli': '微风级', 'fengxiang': '南风', 'type': '多云', 'date': '16日星期五', 'high': '高温 5℃'}], 'aqi': '230', 'wendu': '3', 'city': '北京', 'yesterday': {'low': '低温 -1℃', 'fl': '微风', 'fx': '南风', 'type': '霾', 'date': '11日星期日', 'high': '高温 4℃'}}, 'desc': 'OK', 'status': 1000} <class 'dict'>


2,在文件中操作

  1. import json
  2. li = [11,22,33]
  3. res = json.dump(li, open("f1.txt", "w"))
  4. res1 = json.load(open("f1.txt", "r"))
  5. print(res)
  6. print(res1,type(res1))


  7. 结果如下:
  8. None
  9. [11, 22, 33] <class 'list'>


二,pickle



1,在内存中操作

  1. li = [11, 22, 33]
  2. res = pickle.dumps(li)
  3. res1 = pickle.loads(res)
  4. print(res)
  5. print(res1, type(res1))

  6. 结果如下:

  7. b'\x80\x03]q\x00(K\x0bK\x16K!e.'
  8. [11, 22, 33] <class 'list'>

2,    在文件中操作

  1. li = [11, 22, 33]
  2. pickle.dump(li,open("f1.txt","wb"))            #python3对文件操作时只支持字节
  3. res = pickle.load(open("f1.txt","rb"))
  4. print(res,type(res))

  5. 结果如下:

  6. [11, 22, 33] <class 'list'>

总结:json/pickle的区别。
 1,json更适合跨语言,字符串,基本数据类型
 2,pick仅适用于python,pickle适合python所有类型的序列化



三,time模块


时间相关的操作,时间有三种表示方式:

时间戳               1970年1月1日之后的秒,即:time.time()
格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()

time模块

  1. print(time.time()) #返回当前系统时间戳,从1970年1月1日,如:1481544295.9460318
  2. print(time.ctime()) #如:Mon Dec 12 20:04:55 2016,当前系统时间
  3. print(time.ctime(time.time()-86400)) #将时间戳转为字符串格式。如:Sun Dec 11 20:04:55 2016

  4. print(time.gmtime()) #struct_time格式,显示的是格林尼治时间,如下
  5. #time.struct_time(tm_year=2016, tm_mon=12, tm_mday=12, tm_hour=12, tm_min=17, tm_sec=14, tm_wday=0, tm_yday=347, tm_isdst=0)

  6. print(time.gmtime(time.time()-86400)) #将时间戳转换成struct_time格式

  7. time_obj = time.gmtime()
  8. print(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday) #如:2016 12 12

  9. print(time.localtime()) #返回struct_time,但是为本地时间
  10. print(time.localtime(time.time()-86400)) #将时间戳转换为struct_time,但是返回的是本地时间

  11. print(time.mktime(time.localtime())) #将struct_time转换成时间戳格式

  12. #time.sleep(4) #延迟
  13. print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #将struct_time转换成指定格式的时间
  14. print(time.strptime("2016-12-13","%Y-%m-%d")) #将字符串格式转换成struct_time格式,如:2016-12-13 11:00:48



四,datetime模块

datetime模块

  1. print(datetime.date.today()) #输出格式,2016-12-13
  2. print(datetime.date.fromtimestamp(time.time()-86400)) #将时间戳转换成日期格式

  3. current_time = datetime.datetime.now() #
  4. print(current_time) #返回2016-12-13 11:08:36.774114
  5. print(current_time.timetuple()) #返回struct_time格式
  6. print(current_time.replace(2015,10,10)) #输出2015-10-10 11:13:15.649512,把当前系统的日期替换成指定的日期

  7. print(datetime.datetime.strptime("05/01/16 08:51","%d/%m/%y %H:%M")) #将字符串转换成日期格式

  8. #时间的运算
  9. print(datetime.datetime.now() + datetime.timedelta(days=10)) #比现在多10天,如:2016-12-23 12:03:23.206917
  10. print(datetime.datetime.now() + datetime.timedelta(days=-10)) #比现在少10天
  11. print(datetime.datetime.now() + datetime.timedelta(hours=-10)) #比现在少10小时
  12. print(datetime.datetime.now() + datetime.timedelta(minutes=-10)) #比现在少10分钟
阅读(1855) | 评论(0) | 转发(0) |
0

上一篇:模块

下一篇:logging模块

给主人留下些什么吧!~~