一、JSON解析
(一) JOSN处理
1. JSON概述
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。
如果你还不了解 JSON,可以先阅读我们的 JSON 教程。
Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
json.dumps(): 对数据进行编码。 字符串到Json对象
json.loads(): 对数据进行解码。 Json对象到python对象
2. Python 编码为 JSON 类型转换对应表:
Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null
3. JSON 解码为 Python 类型转换对应表:
JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None
(二) 代码概述
"""
JSON概述
JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式。
如果你还不了解 JSON,可以先阅读我们的 JSON 教程。
Python3 中可以使用 json 模块来对 JSON 数据进行编解码,它包含了两个函数:
json.dumps(): 对数据进行编码。 字符串到Json对象
json.loads(): 对数据进行解码。 Json对象到python对象
Python 编码为 JSON 类型转换对应表:
Python JSON
dict object
list, tuple array
str string
int, float, int- & float-derived Enums number
True true
False false
None null
JSON 解码为 Python 类型转换对应表:
JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None
"""
import json
data = {
'no': 1,
'name': 'Runoob',
'url': 'https://www.runoob.com'
}
json_str = json.dumps(data)
print("Python 原始数据:", repr(data))
print("JSON 对象:", json_str)
data1 = {
'no': 1,
'name': 'Runoob',
'url': 'http://www.runoob.com'
}
json_str = json.dumps(data1)
print("Python 原始数据:", repr(data1))
print("JSON 对象:", json_str)
data2 = json.loads(json_str)
print("data2['name']: ", data2['name'])
print("data2['url']: ", data2['url'])
二、日期和时间
(一) 日期和时间
1. Python3 日期和时间
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。
注意:时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
2. 什么是时间元组?
很多Python函数用一个元组装起来的9组数字处理时间:
序号 字段 值
0 4位数年 2008
1 月 1 到 12
2 日 1到31
3 小时 0到23
4 分钟 0到59
5 秒 0到61 (60或61 是闰秒)
6 一周的第几日 0到6 (0是周一)
7 一年的第几日 1到366 (儒略历)
8 夏令时 -1, 0, 1, -1是决定是否为夏令时的标识
上述也就是 struct_time 元组。这种结构具有如下属性:
序号 属性 值
0 tm_year 2008
1 tm_mon 1 到 12
2 tm_mday 1 到 31
3 tm_hour 0 到 23
4 tm_min 0 到 59
5 tm_sec 0 到 61 (60或61 是闰秒)
6 tm_wday 0 到 6 (0是周一)
7 tm_yday 一年中的第几天,1 到 366
8 tm_isdst 是否为夏令时,值有:1(夏令时)、0(不是夏令时)、-1(未知),默认 -1
3. 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 当前时区的名称
%% %号本身
(二) 代码概述
"""
Python3 日期和时间
Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能。
Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间。
时间间隔是以秒为单位的浮点小数。
每个时间戳都以自从 1970 年 1 月 1 日午夜(历元)经过了多长时间来表示。
注意:时间戳单位最适于做日期运算。但是1970年之前的日期就无法以此表示了。太遥远的日期也不行,UNIX和Windows只支持到2038年。
什么是时间元组?
很多Python函数用一个元组装起来的9组数字处理时间:
序号 字段 值
0 4位数年 2008
1 月 1 到 12
2 日 1到31
3 小时 0到23
4 分钟 0到59
5 秒 0到61 (60或61 是闰秒)
6 一周的第几日 0到6 (0是周一)
7 一年的第几日 1到366 (儒略历)
8 夏令时 -1, 0, 1, -1是决定是否为夏令时的标识
上述也就是 struct_time 元组。这种结构具有如下属性:
序号 属性 值
0 tm_year 2008
1 tm_mon 1 到 12
2 tm_mday 1 到 31
3 tm_hour 0 到 23
4 tm_min 0 到 59
5 tm_sec 0 到 61 (60或61 是闰秒)
6 tm_wday 0 到 6 (0是周一)
7 tm_yday 一年中的第几天,1 到 366
8 tm_isdst 是否为夏令时,值有:1(夏令时)、0(不是夏令时)、-1(未知),默认 -1
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 当前时区的名称
%% %号本身
"""
import time
ticks = time.time()
print("当前时间戳为:", ticks)
print("本地时间为:",
time.localtime())
print("本地时间为:", time.localtime(
time.time()))
def print_time(seconds):
"""
将秒数转换为时间格式
:param seconds:
:return:
"""
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print("%d:%02d:%02d" % (h, m, s))
print_time(ticks)
localtime = time.asctime(time.localtime(time.time()))
print("本地时间为:", localtime)
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
import calendar
print("输出日历",calendar.month(2024, 6))