首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过未知深度JSON结构格式化生成器解析的返回值

如何通过未知深度JSON结构格式化生成器解析的返回值
EN

Stack Overflow用户
提问于 2022-04-29 21:36:48
回答 1查看 49关注 0票数 0

tl;dr:

为什么这是-> dict(yield dict(name=new_key, url=d['url']).items())呢?

这不是-> dict(yield dict(new_key = d['url']).items())

所涉全部守则如下:

代码语言:javascript
复制
import json

with open(".\OsintFrameWork.json", "r") as file:
    osintFramework = json.load(file)

from collections.abc import MutableMapping

def _flatten_dict_gen(d, parent_key, sep):
    if isinstance(d, dict):
       new_key = parent_key + sep + d['name'] if parent_key else d['name'] 
       if isinstance(d.get('children'), list):
           yield from flatten_dict(d['children'], new_key, sep=sep).items()
       else:
           # # This works
           yield dict(name=new_key, url=d['url']).items()
           # # The following doesn't work:
           # yield dict(new_key = d['url']).items()
    else:
        for k in d:
            yield from flatten_dict(k, parent_key, sep=sep).items()

def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '-'):
    return dict(_flatten_dict_gen(d, parent_key, sep))

new_dict = flatten_dict(osintFramework)
clean_dict = {item[1]: value[1] for item,value in new_dict.items()}
__import__('pprint').pprint(clean_dict)

所讨论的数据来自https://osintframework.com/arf.json

这个问题涉及_flatten_dict_gen的第二个收益陈述。

为什么我必须使用一个数据理解clean_dict来获得我想要的输出?

我试过yield dict(new_key = d['url']).items()我也试过yield {new_key : d['url']}.items()

通常,我尝试过的任何其他方式都会得到ValueError: dictionary update sequence element #0 has length x; 2 is required

所以把它都煮了下去:

如何将new_key=d['url']作为外部dict()函数的关键字参数

清洁:

代码语言:javascript
复制
{'OSINT Framework-Archives-Data Leaks-Cryptome': 'http://cryptome.org/',
 'OSINT Framework-Archives-Data Leaks-Databases.Today': 'https://databases.today/',
 'OSINT Framework-Archives-Data Leaks-Weleakinfo': 'https://search.weleakinfo.com/',
 'OSINT Framework-Archives-Data Leaks-WikiLeaks': 'https://wikileaks.org/',
 'OSINT Framework-Archives-Other Media-Library of Congress: Digitized Newspapers - 1836-1922': 'http://chroniclingamerica.loc.gov/',
 'OSINT Framework-Archives-Other Media-Library of Congress: Newspaper Directory - 1690-Present': 'http://chroniclingamerica.loc.gov/search/title
s/',
 'OSINT Framework-Archives-Other Media-TV Closed Caption Search': 'https://archive.org/details/tv', ....

不清洗:

代码语言:javascript
复制
{('name', 'OSINT Framework-Archives-Data Leaks-Cryptome'): ('url',
                                                            'http://cryptome.org/'),
 ('name', 'OSINT Framework-Archives-Data Leaks-Databases.Today'): ('url',
                                                                   'https://databases.today/'),
 ('name', 'OSINT Framework-Archives-Data Leaks-Weleakinfo'): ('url',
                                                              'https://search.weleakinfo.com/'),
 ('name', 'OSINT Framework-Archives-Data Leaks-WikiLeaks'): ('url',
                                                             'https://wikileaks.org/'),
EN

回答 1

Stack Overflow用户

发布于 2022-04-29 23:22:47

我的版本

生成器flatten_dict i中的

  • 使用相同的生成器_flatten_dict_gen来运行产量,而不是使用函数_flatten_dict_gen

生成器中的

  • 我删除了所有items(),我可以使用items()

  • 它生成字典列表,因此我不得不更改代码flatten_dict,将该列表转换为单一字典

现在我不需要clean_dict

代码语言:javascript
复制
import json
from pprint import pprint
from collections.abc import MutableMapping
import requests

response = requests.get('https://osintframework.com/arf.json')
osintFramework = response.json()

#with open(".\OsintFrameWork.json", "r") as file:
#    osintFramework = json.load(file)

def _flatten_dict_gen(d, parent_key, sep):
    if isinstance(d, dict):
       new_key = parent_key + sep + d['name'] if parent_key else d['name']
       #print('new_key:', new_key)
       
       if isinstance(d.get('children'), list):
           yield from _flatten_dict_gen(d['children'], new_key, sep=sep)
       else:
           yield {new_key: d['url']}
    else:
        for k in d:
            yield from _flatten_dict_gen(k, parent_key, sep=sep)

def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '-'):
    return {key:val for item in _flatten_dict_gen(d, parent_key, sep) for key, val in item.items()}

new_dict = flatten_dict(osintFramework)

pprint(new_dict)

flatten_dic也可以

代码语言:javascript
复制
def flatten_dict(d: MutableMapping, parent_key: str = '', sep: str = '-'):
    result = {}
    for item in _flatten_dict_gen(d, parent_key, sep):
        result.update(item)
    return result
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72063912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档