Arrow是一个更加智能的Python时间处理库。它实现并更新日期时间类型,支持创建、操作、格式化和转换日期、时间和时间戳,可以使用更少导入和代码处理日期和时间。
import arrow
# 返回时区的时间 2021-07-01T11:00:07.830552+08:00
print(arrow.now())
# 年
print(arrow.now().year)
# 月
print(arrow.now().month)
# 日
print(arrow.now().day)
# 返回当前时区时间:2021-07-01 11:02:09.333066+08:00
print(arrow.now().datetime)
# 返回当前时区的年月日
print(arrow.now().date())
# 获取指定时区时间 上海时区:'Asia/Shanghai',美国时区:'US/Pacific'
print(arrow.now('US/Pacific').datetime) # 2021-06-30 20:22:09.223668-07:00
print(arrow.now('Asia/Shanghai').datetime) #2021-07-01 11:22:09.223668+08:00
#获取时间戳 :1625108805.093209 ,不管你是否指定地区,默认时区都不是北京时间,因此会少8个小时,因此我们需要加上8个小时
print(arrow.now('Asia/Shanghai').timestamp(),'上海') # 1625109948.842313 上海
print(arrow.now().timestamp(),'没加') # 1625109948.842313 没加
print(arrow.now().timestamp()+28800,'加了') # 正确时间1625138748.842313 加了
# Arrow对象转换未字符串时间 2021-07-01 11:09:44
print(arrow.now().format(fmt='YYYY-MM-DD HH:mm:ss'))
#时间戳转换为日期
timeStamp = arrow.now().timestamp()+28800
i = arrow.get(timeStamp)
print(i.format('YYYY-MM-DD HH:mm:ss')) # 2021-07-01 11:25:48
# 当前时间的前一年,1个月前,2周前,3天后,2小时后时间 [years, months, days, hours, minutes, seconds, microseconds, weeks, quarters, weekday]
print(arrow.now().shift(years=-1, months=-1, weeks=-2, days=3, hours=2).format()) # 2020-05-21 13:41:07+08:00
jsonpath用来解析json数据,是一种简单的方法来提取给定JSON文档的部分内容。它提供了类似正则表达式的语法,可以解析复杂的嵌套数据结构,可以非常方便的提取接口返回的数据信息。
from jsonpath import jsonpath
# 如果匹配不到返回False
dic = {'语法规则字符串':1111}
ret = jsonpath(dic, '语法规则字符串') # [1111]
print(ret)
new_dic = {
"store": {
"book": [{
"category": " reference",
"author": "Nigel Rees",
"title": "sayings of the century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "3.R. R. Tolkien",
"title": "The Lord of the Rings ",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {"color": " red",
"price": 19.95
},
"expensive": 10
}
}
# 获取所有key=isbn的值 $..isbn: $代表最外层 ..代表模糊匹配
print(jsonpath(new_dic,'$..isbn')) # ['0-553-21311-3', '0-395-19395-8']
# 获取store下book下的所有author值 $最外层 .代表下一层(子节点) book[*] book里面所有的 .子节点 key=author
print(jsonpath(new_dic ,'$.store.book[*].author'))
# 获取store下以及所有子节点下的所有price
print(jsonpath(new_dic, '$.store..price'))
# 获取book数组的第3个值 [2]代表第三个值,从0开始
print(jsonpath(new_dic, '$..book[2]'))
#获取book数组的第一、第二的值 两种方式:1.直接在列表传对应索引, 2.列表切片的方式(注意:是左包含,右不包含)
print(jsonpath(new_dic, '$..book[:2]'))
print(jsonpath(new_dic, '$..book[0,1]'))
# 获取book数组从索引2后的所有数据
print(jsonpath(new_dic, '$..book[2:]'))
# 获取所有节点以及子节点中book数组包含key=isbn的对应字典 book[] book里面的数据, ?()过滤的操作, @ 使用过滤谓词来处理当前节点
print(jsonpath(new_dic, '$..book[?(@.isbn)]')) #这样写查出来就是book里面子节点中含有key=isbn的字典
# 获取store下book数组中price < 10的对应字典
print(jsonpath(new_dic, '$.store.book[?(@.price < 10)]')) #[{'category': ' reference', 'author': 'Nigel Rees', 'title': 'sayings of the century', 'price': 8.95}, {'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99}]