python 日期时间和Json解析

一、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

(二) 代码概述

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# @Date : 2024/6/17 10:54
# @Author : water
# @Description : json  解析器


"""
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
"""

# json.dumps 与 json.loads 实例
import json

# Python 字典类型转换为 JSON 对象
data = {
    'no': 1,
    'name': 'Runoob',
    'url': 'https://www.runoob.com'
}

json_str = json.dumps(data)
print("Python 原始数据:", repr(data))  # Python 原始数据: {'no': 1, 'name': 'Runoob', 'url': 'https://www.runoob.com'}
print("JSON 对象:", json_str)  # JSON 对象: {"no": 1, "name": "Runoob", "url": "https://www.runoob.com"}


# 将一个JSON编码的字符串转换回一个Python数据结构:
# Python 字典类型转换为 JSON 对象
data1 = {
    'no': 1,
    'name': 'Runoob',
    'url': 'http://www.runoob.com'
}

json_str = json.dumps(data1)
print("Python 原始数据:", repr(data1))
print("JSON 对象:", json_str)

# 将 JSON 对象转换为 Python 字典
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          当前时区的名称
%%          %号本身

(二) 代码概述

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# @Date : 2024/6/17 11:13
# @Author : water
# @Description : 时间处理
"""
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)  # 当前时间戳为: 1718594121.303148
print("本地时间为:",
      time.localtime())  # 本地时间为: time.struct_time(tm_year=2024, tm_mon=6, tm_mday=17, tm_hour=11, tm_min=16, tm_sec=59, tm_wday=0, tm_yday=169, tm_isdst=0)
print("本地时间为:", time.localtime(
    time.time()))  # 本地时间为: time.struct_time(tm_year=2024, tm_mon=6, tm_mday=17, tm_hour=11, tm_min=16, tm_sec=59, tm_wday=0, tm_yday=169, tm_isdst=0)


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)  # 本地时间为: Mon Jun 17 11:27:45 2024

# 格式化日期
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))  # 2024-06-17 11:27:45

# Calendar 模块有很广泛的方法用来处理年历和月历,例如打印某月的月历
# 获取某月日历
import calendar
print("输出日历",calendar.month(2024, 6))

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值