目录

09-Python进阶-JSON-数据解析日期和时间

09 Python进阶: JSON 数据解析、日期和时间

JSON 数据解析

JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。

Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:

json.dumps(): 对数据进行编码。

json.loads(): 对数据进行解码。

https://i-blog.csdnimg.cn/blog_migrate/1d90e1a897acd8fc7eab584f11d99dc9.png

Python 编码为 JSON 类型转换对应表

https://i-blog.csdnimg.cn/blog_migrate/3aeee7241516250daaade35471688cea.png

JSON 解码为 Python 类型转换对应表

https://i-blog.csdnimg.cn/blog_migrate/351ff56d481c94aa0f2bc89f25fef8d0.png

json.dumps 与 json.loads 实例

当涉及到Python中的JSON数据处理时, json.dumps()json.loads() 是两个常用的函数,分别用于将Python对象转换成JSON格式的字符串和将JSON格式的字符串转换成Python对象。下面是一个简单的示例:

import json

# 定义一个Python字典对象
data = {
    "name": "Alice",
    "age": 30,
    "city": "Shanghai"
}

# 使用json.dumps()将Python对象转换成JSON字符串
json_str = json.dumps(data)
print("JSON字符串:", json_str)

# 使用json.loads()将JSON字符串转换成Python对象
data_parsed = json.loads(json_str)
print("Python对象:", data_parsed)

在上面的示例中,我们先定义了一个Python字典对象,然后使用 json.dumps() 将其转换为JSON格式的字符串,最后再使用 json.loads() 将该JSON字符串转换回Python对象。

日期和时间

Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。

Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。

时间间隔是以秒为单位的浮点小数。

每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。

什么是时间元组?

https://i-blog.csdnimg.cn/blog_migrate/56d361417b9e569f95d7806feafe291d.png

https://i-blog.csdnimg.cn/blog_migrate/6b1822db7525315d309ddec450847b13.png

获取当前时间

从返回浮点数的时间戳方式向时间元组转换,只要将浮点数传递给如localtime之类的函数。

#!/usr/bin/python3

import time

localtime = time.localtime(time.time())
print ("本地时间为 :", localtime)

获取格式化的时间

最简单的获取可读的时间模式的函数是asctime():

#!/usr/bin/python3

import time

localtime = time.asctime( time.localtime(time.time()) )
print ("本地时间为 :", localtime)

格式化日期

可以使用 time 模块的 strftime 方法来格式化日期:

#!/usr/bin/python3

import time

# 格式化成2016-03-20 11:45:39形式
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

# 格式化成Sat Mar 28 22:24:24 2016形式
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
  
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))

python中时间日期格式化符号:

%y 两位数的年份表示(00-99)

%Y 四位数的年份表示(000-9999)

%m 月份(01-12)

%d 月内中的一天(0-31)

%H 24小时制小时数(0-23)

%I 12小时制小时数(01-12)

%M 分钟数(00=59)

%S 秒(00-59)

%a 本地简化星期名称

%A 本地完整星期名称

%b 本地简化的月份名称

%B 本地完整的月份名称

%c 本地相应的日期表示和时间表示

%j 年内的一天(001-366)

%p 本地A.M.或P.M.的等价符

%U 一年中的星期数(00-53)星期天为星期的开始

%w 星期(0-6),星期天为星期的开始

%W 一年中的星期数(00-53)星期一为星期的开始

%x 本地相应的日期表示

%X 本地相应的时间表示

%Z 当前时区的名称

%% %号本身

获取某月日历

#!/usr/bin/python3

import calendar

cal = calendar.month(2016, 1)
print ("以下输出2016年1月份的日历:")
print (cal)

https://i-blog.csdnimg.cn/blog_migrate/a1a9f03364f3deb600188692e26714ba.png

Time 模块

Time 模块包含了以下内置函数,既有时间处理的,也有转换时间格式的

https://i-blog.csdnimg.cn/blog_migrate/d811d3c92ede717e015d032d82a4d258.png

https://i-blog.csdnimg.cn/blog_migrate/1a1bc28939c083db6cb5d1ec7819d363.png

https://i-blog.csdnimg.cn/blog_migrate/3f9ff4fb366fe637bd4820bc93416eda.png

Time模块包含了以下2个非常重要的属性:

https://i-blog.csdnimg.cn/blog_migrate/b0062e6455262a9cac7c324519636091.png

日历(Calendar)模块

此模块的函数都是日历相关的,例如打印某月的字符月历。

星期一是默认的每周第一天,星期天是默认的最后一天。更改设置需调用calendar.setfirstweekday()函数。模块包含了以下内置函数:

https://i-blog.csdnimg.cn/blog_migrate/c5c2cda1560e544ede73459aa7629f53.png

https://i-blog.csdnimg.cn/blog_migrate/54a567aeb4d3213a4c04c7666abc6b1f.png

其他相关模块和函数

在Python中,其他处理日期和时间的模块还有:

time 模块

datetime模块

关注我,不迷路,共学习,同进步

https://i-blog.csdnimg.cn/blog_migrate/31f2d385e6dddf8cff80f2a540a48c48.png