字典操作_jsonpath
1.字典的基本操作:
#! /usr/bin/env python # -*- coding:utf-8 -*- import operator #___________________获取字典的键和值_________________________________ arr = {'a': '苹果', 'b': '三星', 'c': '华为', 'd': '谷歌'} for (key,value) in arr.items():#items以列表返回可遍历的(键, 值)序列(二维数组) #print(arr.items()) #print("这是key:" + key) print("这是%s:"%(key),value) # #____________________获取字典的值____________________________________ arr11 = {'a': '苹果', 'b': '三星', 'c': '华为', 'd': '谷歌'} for i in arr11: print("获取字典的值",arr11[i]) #______________________字典转列表______________________________________ arr22 = {'a': '苹果', 'b': '三星', 'c': '华为', 'd': '谷歌'} ##key和value直接转嵌套列表 my_list = list(arr22.items()) print("字典key和value直接转列表",my_list) #单独key或value转字典 my_list1=list(arr22.keys())#获取字典所有键 print("list格式打印字典所有的key",list(my_list1)) #________________获取所有键、值_________________________________________ aa=arr.keys()#获取字典所有键 print("list格式打印字典所有的key",list(aa)) aa1=arr.values()#获取字典所有值 print("打印字典所有值",aa1) #_________________________字典判断_____________________________________ arr33 = {'a': '苹果', 'b': '三星', 'c': '华为', 'd': '谷歌'} #判断包含 if "a" in arr33: print("arr33字典包含key的a") #判断字典为空 if arr33: print("字典不为空") if len(arr33) >0: print("字典不为空") #___________________________获取不存在的key会报错___________________________________ my_dict = {'a': 1, 'b': 2, 'c': 3} # 使用get()方法获取字典的key value = my_dict.get('d', 0) print(value) # 输出: 0 # 使用get()方法获取字典的key,不设置默认值 value = my_dict.get('d') print(value) # 输出: None #________________字典常用函数———————————————————————————————— arr2={'chen': '你好', 'wei': '大家', 'www': 123} ss=operator.eq(arr, arr2)#eq是比较两个字典的元素是否一致(python和python3不同,python2用cmp()) # print(ss) # print(len(arr))#计算字典元素个数,即键的总数 print(str(arr))#以字符串的方式打印字典 print ("Value : %s" % arr.get('ee', None))#返回指定键的值,如果值不在字典中返回none # print ("Value : %s" % arr.has_key('a'))#python2(如果键在字典里返回true,否则false) print("Value : ",arr.__contains__('a'))#python3(如果键在字典里返回true,否则false)
2.列表嵌套一层字典_并获取字典的key、value
#!/usr/bin/env python # encoding: utf-8 chen=[{'chen':'chenwei','anme':55},{'age':523,'pp':'eeeeeeee'}] #获取key list_key=[key for item in chen for key in item] print('获取所有的key:',list_key) #获取所有的value list_value=[item[key] for item in chen for key in item] print('获取所有的value',list_value) #拆分嵌套字典 for i in range(len(chen)): print(chen[i])
3.字典嵌套处理
#!/usr/bin/env python # encoding: utf-8 dict={'log_id': 5891599090191187877, 'result_num': 1, 'result': [{'probability': 0.9882395267486572, 'top': 205, 'height': 216, 'classname': 'Face', 'width': 191, 'left': 210},{'name':'chenyang','age':25,'sg':170}]} print(dict['log_id']) print('字典中嵌套的list:',dict['result'])#打印字典中嵌套的list(列表中又嵌套字典) #获取dict['result']列表中所有的key list_key=[key for item in dict['result'] for key in item] print('列表中所有的key:',list_key)
4.字典拆分两个list排序_并对不足的补0
class Dict_list(object): def __init__(self,_dict): qu_dao,api,reup=list(_dict['qu_dao'].keys()),list(_dict['api'].keys()),list(_dict['recommended'].keys()) _max=[] if len(qu_dao) >= len(api) and len(qu_dao) >=len(reup): _max.extend(qu_dao) elif len(api) >= len(qu_dao) and len(api) >= len(reup): _max.extend(api) else: _max.extend(reup) _max.sort()#sort对列表排序 print('获取最长日期:{}'.format(_max)) self._dict=_max #获取嵌套字典最长的key def statistics(self,bb): value_list=[] index_list=[] for i in bb: if i[0] in self._dict: value_list.append(i[1]) index_list.append(self._dict.index(i[0])) squares=[x for x in range(len(self._dict))]#统计天数list的下标 index_0=list(set(squares).difference(set(index_list))) #print(index_0)#需要替换为0的下标 for i in range(len(index_0)): value_list.insert(index_0[i],0) return value_list if __name__ == '__main__': nesting_dict= {'api': {'2022-03-09': 2, '2022-03-15': 7, '2022-03-13': 10, '2022-03-14': 7, '2022-03-10': 1, '2022-03-16': 10}, 'recommended': {'2022-03-15': 7, '2022-03-16': 6, '2022-03-13': 6}, 'qu_dao': {'2022-03-15': 6, '2022-03-16': 3, '2022-03-13': 13, '2022-03-14': 9}} aa=[('2022-03-13', 13), ('2022-03-14', 9), ('2022-03-15', 6), ('2022-03-16', 3)] bb=[('2022-03-13', 6), ('2022-03-15', 7), ('2022-03-16', 6)] cc=[('2022-03-09', 2), ('2022-03-10', 1), ('2022-03-13', 10), ('2022-03-14', 7), ('2022-03-15', 7), ('2022-03-16', 10)] _object=Dict_list(nesting_dict) value1=_object.statistics(aa) value2=_object.statistics(bb) print(value1) print(value2)
5.不规则数据&带等号数据转json或字典
#_________________________________带等号数据转字典_______________________________________________________________ string = """{inventory_catalog=MANUAL, inventory_type=['INVENTORY_AWEME_FEED','INVENTORY_VIDEO_FEED'], union_video_type=null}""" list_str=string.split(" ") print("直接按照等号分割",list_str) delivery_range={} for i in list_str: str_1=i.replace("{","").rstrip(",").rstrip("}") key_name, value_name = str_1.split("=") if value_name=="null": value_name=None delivery_range.update({key_name:value_name}) if 'inventory_type' in delivery_range: value_name = eval(delivery_range["inventory_type"]) delivery_range['inventory_type']= value_name print("处理后字典:",delivery_range) print("处理后json:",json.dumps(delivery_range)) ##__________________________________不规则数据转成字典____________________________________________________________ import re import ast s = "{inventory_catalog=MANUAL, inventory_type=['INVENTORY_AWEME_FEED','INVENTORY_VIDEO_FEED'], union_video_type=null}" # 提取中括号内的字符串并替换分隔符 pattern = r'\[([^\[\]]*)\]' substrings = re.findall(pattern, s) for substr in substrings: s = s.replace(substr, substr.replace(',', '#')) # 提取键值对 pattern = r'([^=,{}\s]+)\s*=\s*([^={},]+)' pairs = re.findall(pattern, s) # 构建字典 result = {} for key, value in pairs: if value == 'null': value = None elif value.startswith('['): value = ast.literal_eval(value.replace('#', ',')) result[key] = value print("转换后的字典",result)
#_____________________________________________________第三方模块__________________________________________________________________________
jsonpath模块:
用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括:Javascript, Python, PHP 和 Java。
安装方法:pip install jsonpath
1.jsonpath获取多层嵌套字典的值_匹配处理
#!/usr/bin/python # -*- coding: UTF-8 -*- from jsonpath import jsonpath ''' 注意:先要把数据转成json格式:json.dumps() ''' result = { "header": { "code": 1, "date":"2" }, "body": { "isTmpUser": 6, "lastActiveTime": { "date": 30, "timezoneOffset": 480 }, "name":"test123" }, "code": 12 } print('常规写法取值',result['body']['lastActiveTime']['date']) #当前节点匹配 result_data = jsonpath(result['body']['lastActiveTime'],'$.date') print("当前节点匹配data",result_data) ##获取子集key的值 result_data = jsonpath(result,'$.body.lastActiveTime.date') #date为30 #获取lastActiveTime的所有值 result_data = jsonpath(result,'$.body.lastActiveTime[*]') print('jsonpath取值_2',result_data) #获取code<10的字典(不管层级),值必须为int类型 result_data_2 = jsonpath(result,'$..[?(@.code < 10)]')#结果成功为list嵌套字典 print('jsonpath取值_2',result_data_2) #不管位置,选择所有符合条件的 result_data_1 = jsonpath(result,'$..date') print('jsonpath取值_2',result_data_1) #条件匹配后在匹配,然后再匹配孙子节点 result_data = jsonpath(result,"$..[?(@.name=='test123')].lastActiveTime..timezoneOffset") print("当name符合条件,然后返回这些对象中的lastActiveTime属性的所有子孙节点中的timezoneOffset属性值",result_data)
#____________________________________详细json数据案例(高级篇)_____________________________________________________________ weiwei={ "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": "J. R. R. Tolkien", "title": "The Lord of the Rings", "isbn": "0-395-19395-8", "price": 22.99 } ], "bicycle": { "color": "red", "price": 7.95 } }, "expensive": 10 } #不管位置,选择所有符合条件的 result_data_1 = jsonpath(weiwei,'$..price') print('jsonpath取值_1',result_data_1) ##获取store下book下price<10的数据 result_data_2 = jsonpath(weiwei,'$.store.book[?(@.price < 10)]') ##获取所有book下的price<10的数据 result_data_2 = jsonpath(weiwei,'$..book[?(@.price < 10)]') ##获取整个weiwei中price<10的数据 result_data_2 = jsonpath(weiwei,'$..[?(@.price < 10)]') print('jsonpath取值_2',result_data_2) ##获取json中store下book下的所有author值 result_data_3 = jsonpath(weiwei,'$.store.book[*].author') print(result_data_3) ##获取json中store下所有price的值 result_data_4 = jsonpath(weiwei,'$.store..price') print(result_data_4) ##获取json中book数组中包含isbn的所有值 result_data_5 = jsonpath(weiwei,'$..book[?(@.isbn)]') print(result_data_5)
jsonpath-rw模块:
JSON 通常用于与服务端交换数据,在接收服务器数据时一般是字符串。
我们可以使用 JSON.parse() 方法将数据转换为 JavaScript 对象。
安装方法:pip install jsonpath-rw
2.jsonpath-rw模块常用方法
#!/usr/bin/env python # encoding: utf-8 from jsonpath_rw import jsonpath,parse
###########第一种方式 data={"data":{"yesterday":{"date":"9日星期日","high":"高温 1℃","fx":"东北风","low":"低温 -5℃","fl":"<![CDATA[1级]]>","type":"阴"},"city":"北京","forecast":[{"date":"10日星期一","high":"高温 3℃","fengli":"<![CDATA[3级]]>","low":"低温 -8℃","fengxiang":"北风","type":"晴"},{"date":"11日星期二","high":"高温 0℃","fengli":"<![CDATA[1级]]>","low":"低温 -8℃","fengxiang":"东风","type":"晴"},{"date":"12日星期三","high":"高温 4℃","fengli":"<![CDATA[2级]]>","low":"低温 -6℃","fengxiang":"北风","type":"多云"},{"date":"13日星期四","high":"高温 3℃","fengli":"<![CDATA[2级]]>","low":"低温 -7℃","fengxiang":"西北风","type":"晴"},{"date":"14日星期五","high":"高温 2℃","fengli":"<![CDATA[1级]]>","low":"低温 -7℃","fengxiang":"北风","type":"多云"}],"ganmao":"感冒高发期,尽量避免外出,外出戴口罩防护。","wendu":"1"},"status":1000,"desc":"OK"} print(type(data)) #data = {'foo': [{'chen': 'news'}, {'chen': 'music'}]} jsonpath_expr = parse('data[*].ganmao') print([match.value for match in jsonpath_expr.find(data)]) print([match.value for match in jsonpath_expr.find(data)][0])#获取list的第0个下标 ##########第二种方式 data = {'foo': [{'chen': 'news'}, {'chen': 'music'}]} print(type(data)) res='foo[*].chen' jsonpath_expr = parse(res) madle = jsonpath_expr.find(data) print([math.value for math in madle]) #__________________________________________________案例:获取接口下的指定字段__________________________________________________________________ import json import requests from jsonpath_rw import jsonpath,parse res=requests.get('https://qqlykm.cn/api/api/tq.php?city=北京') res_test=res.text chen_text=json.loads(res_test)#把响应转成字典 print(chen_text) print(type(chen_text))#查看类型 json_exe = parse('data[*].ganmao')#表达式 ##分两步取值 madle = json_exe.find(chen_text) print([math.value for math in madle][0]) #直接一步取值 #print([match.value for match in json_exe.find(chen_text)][0])#取list下标0
相关连接:
https://www.cnblogs.com/wynjauu/articles/9556396.html ...................jsonpath匹配操作
https://www.cnblogs.com/miqi1992/p/8081244.html ..........................jsonpath匹配操作
https://www.cnblogs.com/ronyjay/p/11114848.html.............................jsonpath-rw处理Json对象
https://www.cnblogs.com/wongbingming/p/6896886.html..................jsonpath-rw处理Json对象2 ,,json对比 ,,数据格式在线
https://www.json.cm/regex/ ...............................................................正则表达式测试 ,,文本处理工具 ,,统计字符串,,字符串查工具,,字符串工具
https://www.site24x7.com/zhcn/tools/jsonpath-finder-validator.html ...jsonpath提取器工具(支持切片)