鱼C论坛

 找回密码
 立即注册
查看: 2088|回复: 8

[已解决]字典嵌套字典如何层层解开获取里面的数据

[复制链接]
发表于 2023-2-7 14:09:57 | 显示全部楼层 |阅读模式
50鱼币
  1. dict = {
  2.         'result': {
  3.                 'categorys': [{
  4.                         'categorynum': '001',
  5.                         'count': '55',
  6.                         'categoryname': '重庆公共资源'
  7.                 }],
  8.                 'totalcount': 55,
  9.                 'records': [{
  10.                         'pubinwebdate': '2022-10-11 17:54:26',
  11.                         'titlenew': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  12.                         'infoa': '',
  13.                         'title': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  14.                         'infod': '50000120220708001030101',
  15.                         'infoc': '市级',
  16.                         'categorytype': '工程招投标',
  17.                         'content': '',
  18.                         'rowid': '6592031',
  19.                         'highlight': {},
  20.                         'score': None,
  21.                         'istop': '0',
  22.                         'kaibiaodate': '2022-10-11 17:54:25',
  23.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  24.                         'id': 'cqggzyc6f8bf56-5b35-4391-bb06-3ba25aeccecb004002002010_001',
  25.                         'sysscore': '0',
  26.                         'categorynum': '004002002010',
  27.                         'infoid': 'c6f8bf56-5b35-4391-bb06-3ba25aeccecb',
  28.                         'categorytype2': '答疑补遗',
  29.                         'zhuanzai': '来源于电子招投标系统',
  30.                         'sysclicktimes': 0,
  31.                         'ordernum': '0',
  32.                         'webdate': '2022-10-11 23:59:59',
  33.                         'projectno': '',
  34.                         'customtitle': '',
  35.                         'syscategory': '001',
  36.                         'linkurl': '',
  37.                         'tktype': '',
  38.                         'infodate': '2022-10-11 17:54:26'
  39.                 }, {
  40.                         'pubinwebdate': '2022-10-11 16:19:05',
  41.                         'titlenew': '茶惠大道二标段检测工程的中标候选人公示',
  42.                         'infoa': '',
  43.                         'title': '茶惠大道二标段检测工程的中标候选人公示',
  44.                         'infod': '50000120220913025090101',
  45.                         'infoc': '市级',
  46.                         'categorytype': '工程招投标',
  47.                         'content': '',
  48.                         'rowid': '6591187',
  49.                         'highlight': {},
  50.                         'score': None,
  51.                         'istop': '0',
  52.                         'kaibiaodate': '2022-10-11 00:00:00',
  53.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  54.                         'id': 'cqggzyc5ab4832-7c72-4465-bb44-e2663fb6c007004002003002_001',
  55.                         'sysscore': '0',
  56.                         'categorynum': '004002003002',
  57.                         'infoid': 'c5ab4832-7c72-4465-bb44-e2663fb6c007',
  58.                         'categorytype2': '中标候选人公示',
  59.                         'zhuanzai': '来源于电子招投标系统',
  60.                         'sysclicktimes': 0,
  61.                         'ordernum': '0',
  62.                         'webdate': '2022-10-11 23:59:59',
  63.                         'projectno': '',
  64.                         'customtitle': '',
  65.                         'syscategory': '001',
  66.                         'linkurl': '',
  67.                         'tktype': '',
  68.                         'infodate': '2022-10-11 16:20:26'
  69.                 }, {
  70.                         'pubinwebdate': '2023-01-13 16:32:13',
  71.                         'titlenew': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  72.                         'infoa': '',
  73.                         'title': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  74.                         'infod': '5002262038943582210100025',
  75.                         'infoc': '荣昌区',
  76.                         'categorytype': '中介超市',
  77.                         'content': '',
  78.                         'rowid': '7137421',
  79.                         'highlight': {},
  80.                         'score': None,
  81.                         'istop': '0',
  82.                         'kaibiaodate': '2022-10-10 09:28:34',
  83.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  84.                         'id': 'cqggzyPNII_5002262038943582210100025004010001_001',
  85.                         'sysscore': '0',
  86.                         'categorynum': '004010001',
  87.                         'infoid': 'PNII_5002262038943582210100025',
  88.                         'categorytype2': '采购公告',
  89.                         'zhuanzai': '',
  90.                         'sysclicktimes': 0,
  91.                         'ordernum': '0',
  92.                         'webdate': '2022-10-10 09:28:34',
  93.                         'projectno': '',
  94.                         'customtitle': '',
  95.                         'syscategory': '001',
  96.                         'linkurl': '',
  97.                         'tktype': '',
  98.                         'infodate': '2023-01-13 16:32:13'
  99.                 }],
  100.                 'scorllId': '0;0;1665394114000;7137421;',
  101.                 'executetime': '0.245'
  102.         }
  103. }
  104. dict1=(dict.values())
复制代码

多层字典嵌套的数据,请问如何解开是最快捷的方法,感谢大佬们了
最佳答案
2023-2-7 14:09:58
uupppo 发表于 2023-2-7 14:10
我现在需要'titlenew'和'infod'的值

  1. data = {
  2.         'result': {
  3.                 'categorys': [{
  4.                         'categorynum': '001',
  5.                         'count': '55',
  6.                         'categoryname': '重庆公共资源'
  7.                 }],
  8.                 'totalcount': 55,
  9.                 'records': [{
  10.                         'pubinwebdate': '2022-10-11 17:54:26',
  11.                         'titlenew': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  12.                         'infoa': '',
  13.                         'title': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  14.                         'infod': '50000120220708001030101',
  15.                         'infoc': '市级',
  16.                         'categorytype': '工程招投标',
  17.                         'content': '',
  18.                         'rowid': '6592031',
  19.                         'highlight': {},
  20.                         'score': None,
  21.                         'istop': '0',
  22.                         'kaibiaodate': '2022-10-11 17:54:25',
  23.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  24.                         'id': 'cqggzyc6f8bf56-5b35-4391-bb06-3ba25aeccecb004002002010_001',
  25.                         'sysscore': '0',
  26.                         'categorynum': '004002002010',
  27.                         'infoid': 'c6f8bf56-5b35-4391-bb06-3ba25aeccecb',
  28.                         'categorytype2': '答疑补遗',
  29.                         'zhuanzai': '来源于电子招投标系统',
  30.                         'sysclicktimes': 0,
  31.                         'ordernum': '0',
  32.                         'webdate': '2022-10-11 23:59:59',
  33.                         'projectno': '',
  34.                         'customtitle': '',
  35.                         'syscategory': '001',
  36.                         'linkurl': '',
  37.                         'tktype': '',
  38.                         'infodate': '2022-10-11 17:54:26'
  39.                 }, {
  40.                         'pubinwebdate': '2022-10-11 16:19:05',
  41.                         'titlenew': '茶惠大道二标段检测工程的中标候选人公示',
  42.                         'infoa': '',
  43.                         'title': '茶惠大道二标段检测工程的中标候选人公示',
  44.                         'infod': '50000120220913025090101',
  45.                         'infoc': '市级',
  46.                         'categorytype': '工程招投标',
  47.                         'content': '',
  48.                         'rowid': '6591187',
  49.                         'highlight': {},
  50.                         'score': None,
  51.                         'istop': '0',
  52.                         'kaibiaodate': '2022-10-11 00:00:00',
  53.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  54.                         'id': 'cqggzyc5ab4832-7c72-4465-bb44-e2663fb6c007004002003002_001',
  55.                         'sysscore': '0',
  56.                         'categorynum': '004002003002',
  57.                         'infoid': 'c5ab4832-7c72-4465-bb44-e2663fb6c007',
  58.                         'categorytype2': '中标候选人公示',
  59.                         'zhuanzai': '来源于电子招投标系统',
  60.                         'sysclicktimes': 0,
  61.                         'ordernum': '0',
  62.                         'webdate': '2022-10-11 23:59:59',
  63.                         'projectno': '',
  64.                         'customtitle': '',
  65.                         'syscategory': '001',
  66.                         'linkurl': '',
  67.                         'tktype': '',
  68.                         'infodate': '2022-10-11 16:20:26'
  69.                 }, {
  70.                         'pubinwebdate': '2023-01-13 16:32:13',
  71.                         'titlenew': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  72.                         'infoa': '',
  73.                         'title': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  74.                         'infod': '5002262038943582210100025',
  75.                         'infoc': '荣昌区',
  76.                         'categorytype': '中介超市',
  77.                         'content': '',
  78.                         'rowid': '7137421',
  79.                         'highlight': {},
  80.                         'score': None,
  81.                         'istop': '0',
  82.                         'kaibiaodate': '2022-10-10 09:28:34',
  83.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  84.                         'id': 'cqggzyPNII_5002262038943582210100025004010001_001',
  85.                         'sysscore': '0',
  86.                         'categorynum': '004010001',
  87.                         'infoid': 'PNII_5002262038943582210100025',
  88.                         'categorytype2': '采购公告',
  89.                         'zhuanzai': '',
  90.                         'sysclicktimes': 0,
  91.                         'ordernum': '0',
  92.                         'webdate': '2022-10-10 09:28:34',
  93.                         'projectno': '',
  94.                         'customtitle': '',
  95.                         'syscategory': '001',
  96.                         'linkurl': '',
  97.                         'tktype': '',
  98.                         'infodate': '2023-01-13 16:32:13'
  99.                 }],
  100.                 'scorllId': '0;0;1665394114000;7137421;',
  101.                 'executetime': '0.245'
  102.         }
  103. }


  104. records = data['result']['records']
  105. for i in records:
  106.     print(f'titlenew: {i["titlenew"]}')
  107.     print(f'infod: {i["infod"]}')
  108.     print('-' * 100)
复制代码

最佳答案

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 14:09:58 | 显示全部楼层    本楼为最佳答案   
uupppo 发表于 2023-2-7 14:10
我现在需要'titlenew'和'infod'的值

  1. data = {
  2.         'result': {
  3.                 'categorys': [{
  4.                         'categorynum': '001',
  5.                         'count': '55',
  6.                         'categoryname': '重庆公共资源'
  7.                 }],
  8.                 'totalcount': 55,
  9.                 'records': [{
  10.                         'pubinwebdate': '2022-10-11 17:54:26',
  11.                         'titlenew': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  12.                         'infoa': '',
  13.                         'title': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  14.                         'infod': '50000120220708001030101',
  15.                         'infoc': '市级',
  16.                         'categorytype': '工程招投标',
  17.                         'content': '',
  18.                         'rowid': '6592031',
  19.                         'highlight': {},
  20.                         'score': None,
  21.                         'istop': '0',
  22.                         'kaibiaodate': '2022-10-11 17:54:25',
  23.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  24.                         'id': 'cqggzyc6f8bf56-5b35-4391-bb06-3ba25aeccecb004002002010_001',
  25.                         'sysscore': '0',
  26.                         'categorynum': '004002002010',
  27.                         'infoid': 'c6f8bf56-5b35-4391-bb06-3ba25aeccecb',
  28.                         'categorytype2': '答疑补遗',
  29.                         'zhuanzai': '来源于电子招投标系统',
  30.                         'sysclicktimes': 0,
  31.                         'ordernum': '0',
  32.                         'webdate': '2022-10-11 23:59:59',
  33.                         'projectno': '',
  34.                         'customtitle': '',
  35.                         'syscategory': '001',
  36.                         'linkurl': '',
  37.                         'tktype': '',
  38.                         'infodate': '2022-10-11 17:54:26'
  39.                 }, {
  40.                         'pubinwebdate': '2022-10-11 16:19:05',
  41.                         'titlenew': '茶惠大道二标段检测工程的中标候选人公示',
  42.                         'infoa': '',
  43.                         'title': '茶惠大道二标段检测工程的中标候选人公示',
  44.                         'infod': '50000120220913025090101',
  45.                         'infoc': '市级',
  46.                         'categorytype': '工程招投标',
  47.                         'content': '',
  48.                         'rowid': '6591187',
  49.                         'highlight': {},
  50.                         'score': None,
  51.                         'istop': '0',
  52.                         'kaibiaodate': '2022-10-11 00:00:00',
  53.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  54.                         'id': 'cqggzyc5ab4832-7c72-4465-bb44-e2663fb6c007004002003002_001',
  55.                         'sysscore': '0',
  56.                         'categorynum': '004002003002',
  57.                         'infoid': 'c5ab4832-7c72-4465-bb44-e2663fb6c007',
  58.                         'categorytype2': '中标候选人公示',
  59.                         'zhuanzai': '来源于电子招投标系统',
  60.                         'sysclicktimes': 0,
  61.                         'ordernum': '0',
  62.                         'webdate': '2022-10-11 23:59:59',
  63.                         'projectno': '',
  64.                         'customtitle': '',
  65.                         'syscategory': '001',
  66.                         'linkurl': '',
  67.                         'tktype': '',
  68.                         'infodate': '2022-10-11 16:20:26'
  69.                 }, {
  70.                         'pubinwebdate': '2023-01-13 16:32:13',
  71.                         'titlenew': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  72.                         'infoa': '',
  73.                         'title': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  74.                         'infod': '5002262038943582210100025',
  75.                         'infoc': '荣昌区',
  76.                         'categorytype': '中介超市',
  77.                         'content': '',
  78.                         'rowid': '7137421',
  79.                         'highlight': {},
  80.                         'score': None,
  81.                         'istop': '0',
  82.                         'kaibiaodate': '2022-10-10 09:28:34',
  83.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  84.                         'id': 'cqggzyPNII_5002262038943582210100025004010001_001',
  85.                         'sysscore': '0',
  86.                         'categorynum': '004010001',
  87.                         'infoid': 'PNII_5002262038943582210100025',
  88.                         'categorytype2': '采购公告',
  89.                         'zhuanzai': '',
  90.                         'sysclicktimes': 0,
  91.                         'ordernum': '0',
  92.                         'webdate': '2022-10-10 09:28:34',
  93.                         'projectno': '',
  94.                         'customtitle': '',
  95.                         'syscategory': '001',
  96.                         'linkurl': '',
  97.                         'tktype': '',
  98.                         'infodate': '2023-01-13 16:32:13'
  99.                 }],
  100.                 'scorllId': '0;0;1665394114000;7137421;',
  101.                 'executetime': '0.245'
  102.         }
  103. }


  104. records = data['result']['records']
  105. for i in records:
  106.     print(f'titlenew: {i["titlenew"]}')
  107.     print(f'infod: {i["infod"]}')
  108.     print('-' * 100)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-7 14:10:52 | 显示全部楼层
我现在需要'titlenew'和'infod'的值
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 15:06:33 | 显示全部楼层
本帖最后由 isdkz 于 2023-2-7 15:07 编辑

  1. data = {
  2.         'result': {
  3.                 'categorys': [{
  4.                         'categorynum': '001',
  5.                         'count': '55',
  6.                         'categoryname': '重庆公共资源'
  7.                 }],
  8.                 'totalcount': 55,
  9.                 'records': [{
  10.                         'pubinwebdate': '2022-10-11 17:54:26',
  11.                         'titlenew': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  12.                         'infoa': '',
  13.                         'title': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  14.                         'infod': '50000120220708001030101',
  15.                         'infoc': '市级',
  16.                         'categorytype': '工程招投标',
  17.                         'content': '',
  18.                         'rowid': '6592031',
  19.                         'highlight': {},
  20.                         'score': None,
  21.                         'istop': '0',
  22.                         'kaibiaodate': '2022-10-11 17:54:25',
  23.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  24.                         'id': 'cqggzyc6f8bf56-5b35-4391-bb06-3ba25aeccecb004002002010_001',
  25.                         'sysscore': '0',
  26.                         'categorynum': '004002002010',
  27.                         'infoid': 'c6f8bf56-5b35-4391-bb06-3ba25aeccecb',
  28.                         'categorytype2': '答疑补遗',
  29.                         'zhuanzai': '来源于电子招投标系统',
  30.                         'sysclicktimes': 0,
  31.                         'ordernum': '0',
  32.                         'webdate': '2022-10-11 23:59:59',
  33.                         'projectno': '',
  34.                         'customtitle': '',
  35.                         'syscategory': '001',
  36.                         'linkurl': '',
  37.                         'tktype': '',
  38.                         'infodate': '2022-10-11 17:54:26'
  39.                 }, {
  40.                         'pubinwebdate': '2022-10-11 16:19:05',
  41.                         'titlenew': '茶惠大道二标段检测工程的中标候选人公示',
  42.                         'infoa': '',
  43.                         'title': '茶惠大道二标段检测工程的中标候选人公示',
  44.                         'infod': '50000120220913025090101',
  45.                         'infoc': '市级',
  46.                         'categorytype': '工程招投标',
  47.                         'content': '',
  48.                         'rowid': '6591187',
  49.                         'highlight': {},
  50.                         'score': None,
  51.                         'istop': '0',
  52.                         'kaibiaodate': '2022-10-11 00:00:00',
  53.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  54.                         'id': 'cqggzyc5ab4832-7c72-4465-bb44-e2663fb6c007004002003002_001',
  55.                         'sysscore': '0',
  56.                         'categorynum': '004002003002',
  57.                         'infoid': 'c5ab4832-7c72-4465-bb44-e2663fb6c007',
  58.                         'categorytype2': '中标候选人公示',
  59.                         'zhuanzai': '来源于电子招投标系统',
  60.                         'sysclicktimes': 0,
  61.                         'ordernum': '0',
  62.                         'webdate': '2022-10-11 23:59:59',
  63.                         'projectno': '',
  64.                         'customtitle': '',
  65.                         'syscategory': '001',
  66.                         'linkurl': '',
  67.                         'tktype': '',
  68.                         'infodate': '2022-10-11 16:20:26'
  69.                 }, {
  70.                         'pubinwebdate': '2023-01-13 16:32:13',
  71.                         'titlenew': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  72.                         'infoa': '',
  73.                         'title': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  74.                         'infod': '5002262038943582210100025',
  75.                         'infoc': '荣昌区',
  76.                         'categorytype': '中介超市',
  77.                         'content': '',
  78.                         'rowid': '7137421',
  79.                         'highlight': {},
  80.                         'score': None,
  81.                         'istop': '0',
  82.                         'kaibiaodate': '2022-10-10 09:28:34',
  83.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  84.                         'id': 'cqggzyPNII_5002262038943582210100025004010001_001',
  85.                         'sysscore': '0',
  86.                         'categorynum': '004010001',
  87.                         'infoid': 'PNII_5002262038943582210100025',
  88.                         'categorytype2': '采购公告',
  89.                         'zhuanzai': '',
  90.                         'sysclicktimes': 0,
  91.                         'ordernum': '0',
  92.                         'webdate': '2022-10-10 09:28:34',
  93.                         'projectno': '',
  94.                         'customtitle': '',
  95.                         'syscategory': '001',
  96.                         'linkurl': '',
  97.                         'tktype': '',
  98.                         'infodate': '2023-01-13 16:32:13'
  99.                 }],
  100.                 'scorllId': '0;0;1665394114000;7137421;',
  101.                 'executetime': '0.245'
  102.         }
  103. }


  104. records = data['result']['records']
  105. for i in records:
  106.     print(f'titlenew: {i["titlenew"]}')
  107.     print(f'infod: {i["infod"]}')
  108.     print('-' * 100)
  109.    
  110.    
  111.    
  112.    
  113.    
  114.    
  115.    
  116.    
  117.    
  118.    
  119. # 下面这段代码用来快速获取键的结构
  120. def get_json_struct(dic, start='', result=[]):
  121.     if isinstance(dic, dict):
  122.         for k in dic:
  123.             get_json_struct(dic[k], f'{start}.{k}', result)
  124.     elif isinstance(dic, tuple) or isinstance(dic, list):
  125.         for i, each in enumerate(dic):
  126.             get_json_struct(each, f'{start}.{i}->index', result)
  127.     else:
  128.         result.append(start[1:])
  129.     return result



  130. titlenews = []
  131. infods = []

  132. for i in get_json_struct(data):
  133.     if 'titlenew' in i:
  134.         titlenews.append(i)
  135.     if 'infod' in i:
  136.         infods.append(i)


  137. print('-' * 100)
  138. print("""
  139.     包含 titlenews 和 infods 的索引结构,其中 ->index 表示这个是列表或元组的索引,而不是字典的键,如无 -> index即为字典的键
  140. """)
  141. print()
  142. print('titlenews:', titlenews)
  143. print()
  144. print('infods:', infods)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-7 15:14:24 | 显示全部楼层

确实可以显示了,但是我没看懂代码,有相应的解析教程吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 15:18:33 | 显示全部楼层
uupppo 发表于 2023-2-7 15:14
确实可以显示了,但是我没看懂代码,有相应的解析教程吗

如果不是很复杂的结构的话自己来一层一层判断它的键就好了,

我没有发现有什么可以剖析字典的层次结构的库,所以写了个递归函数来判断,

递归函数确实不好理解,也不好讲清楚
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-7 15:46:37 | 显示全部楼层
isdkz 发表于 2023-2-7 15:18
如果不是很复杂的结构的话自己来一层一层判断它的键就好了,

我没有发现有什么可以剖析字典的层次结构 ...
  1. import json
  2. dict = {
  3.         'result': {
  4.                 'categorys': [{
  5.                         'categorynum': '001',
  6.                         'count': '55',
  7.                         'categoryname': '重庆公共资源'
  8.                 }],
  9.                 'totalcount': 55,
  10.                 'records': [{
  11.                         'pubinwebdate': '2022-10-11 17:54:26',
  12.                         'titlenew': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  13.                         'infoa': '',
  14.                         'title': '重庆市食品药品检验检测研究院迁(扩)建项目(二期)电梯采购及安装工程答疑补遗文件',
  15.                         'infod': '50000120220708001030101',
  16.                         'infoc': '市级',
  17.                         'categorytype': '工程招投标',
  18.                         'content': '',
  19.                         'rowid': '6592031',
  20.                         'highlight': {},
  21.                         'score': None,
  22.                         'istop': '0',
  23.                         'kaibiaodate': '2022-10-11 17:54:25',
  24.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  25.                         'id': 'cqggzyc6f8bf56-5b35-4391-bb06-3ba25aeccecb004002002010_001',
  26.                         'sysscore': '0',
  27.                         'categorynum': '004002002010',
  28.                         'infoid': 'c6f8bf56-5b35-4391-bb06-3ba25aeccecb',
  29.                         'categorytype2': '答疑补遗',
  30.                         'zhuanzai': '来源于电子招投标系统',
  31.                         'sysclicktimes': 0,
  32.                         'ordernum': '0',
  33.                         'webdate': '2022-10-11 23:59:59',
  34.                         'projectno': '',
  35.                         'customtitle': '',
  36.                         'syscategory': '001',
  37.                         'linkurl': '',
  38.                         'tktype': '',
  39.                         'infodate': '2022-10-11 17:54:26'
  40.                 }, {
  41.                         'pubinwebdate': '2022-10-11 16:19:05',
  42.                         'titlenew': '茶惠大道二标段检测工程的中标候选人公示',
  43.                         'infoa': '',
  44.                         'title': '茶惠大道二标段检测工程的中标候选人公示',
  45.                         'infod': '50000120220913025090101',
  46.                         'infoc': '市级',
  47.                         'categorytype': '工程招投标',
  48.                         'content': '',
  49.                         'rowid': '6591187',
  50.                         'highlight': {},
  51.                         'score': None,
  52.                         'istop': '0',
  53.                         'kaibiaodate': '2022-10-11 00:00:00',
  54.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  55.                         'id': 'cqggzyc5ab4832-7c72-4465-bb44-e2663fb6c007004002003002_001',
  56.                         'sysscore': '0',
  57.                         'categorynum': '004002003002',
  58.                         'infoid': 'c5ab4832-7c72-4465-bb44-e2663fb6c007',
  59.                         'categorytype2': '中标候选人公示',
  60.                         'zhuanzai': '来源于电子招投标系统',
  61.                         'sysclicktimes': 0,
  62.                         'ordernum': '0',
  63.                         'webdate': '2022-10-11 23:59:59',
  64.                         'projectno': '',
  65.                         'customtitle': '',
  66.                         'syscategory': '001',
  67.                         'linkurl': '',
  68.                         'tktype': '',
  69.                         'infodate': '2022-10-11 16:20:26'
  70.                 }, {
  71.                         'pubinwebdate': '2023-01-13 16:32:13',
  72.                         'titlenew': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  73.                         'infoa': '',
  74.                         'title': '关于为【国家畜牧科技城公共实训职教基地(荣昌职教中心工程二期)材料检测服务】 公开选取【工程招标代理】机构的公告',
  75.                         'infod': '5002262038943582210100025',
  76.                         'infoc': '荣昌区',
  77.                         'categorytype': '中介超市',
  78.                         'content': '',
  79.                         'rowid': '7137421',
  80.                         'highlight': {},
  81.                         'score': None,
  82.                         'istop': '0',
  83.                         'kaibiaodate': '2022-10-10 09:28:34',
  84.                         'syscollectguid': 'a2861e5f-8e5d-403d-b746-8abea733b966',
  85.                         'id': 'cqggzyPNII_5002262038943582210100025004010001_001',
  86.                         'sysscore': '0',
  87.                         'categorynum': '004010001',
  88.                         'infoid': 'PNII_5002262038943582210100025',
  89.                         'categorytype2': '采购公告',
  90.                         'zhuanzai': '',
  91.                         'sysclicktimes': 0,
  92.                         'ordernum': '0',
  93.                         'webdate': '2022-10-10 09:28:34',
  94.                         'projectno': '',
  95.                         'customtitle': '',
  96.                         'syscategory': '001',
  97.                         'linkurl': '',
  98.                         'tktype': '',
  99.                         'infodate': '2023-01-13 16:32:13'
  100.                 }],
  101.                 'scorllId': '0;0;1665394114000;7137421;',
  102.                 'executetime': '0.245'
  103.         }
  104. }
  105. a=list((dict['result']['records']))
  106. for i in a:
  107.         print(i)
  108.         for b in i:
  109.                 b=i['titlenew']
  110.                 print(b)
复制代码

大佬递归函数有点复杂,我自己看了下有个见到那的代码可以解决,但是我在使用的时候print(b)出现了很多行,想问问是哪里出了问题
无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-2-7 15:50:44 | 显示全部楼层
uupppo 发表于 2023-2-7 15:46
大佬递归函数有点复杂,我自己看了下有个见到那的代码可以解决,但是我在使用的时候print(b)出现了很多 ...

我那个递归函数只是为了获取结构的,不是获取值的,获取值根本不需要用到递归函数

你那个代码
for i in a:
        print(i)
        for b in i:
                b=i['titlenew']
                print(b)

改成

for i in a:
     b=i['titlenew']
     print(b)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-2-7 16:01:57 | 显示全部楼层
isdkz 发表于 2023-2-7 15:50
我那个递归函数只是为了获取结构的,不是获取值的,获取值根本不需要用到递归函数

你那个代码

感谢大佬了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 02:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表