tl;dr:
为什么这是-> dict(yield dict(name=new_key, url=d['url']).items())呢?
这不是-> dict(yield dict(new_key = d['url']).items())
所涉全部守则如下:
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()函数的关键字参数
清洁:
{'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', ....不清洗:
{('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/'),发布于 2022-04-29 23:22:47
我的版本
生成器flatten_dict i中的
_flatten_dict_gen来运行产量,而不是使用函数_flatten_dict_gen。生成器中的
items(),我可以使用items()flatten_dict,将该列表转换为单一字典。
现在我不需要clean_dict
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也可以
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 resulthttps://stackoverflow.com/questions/72063912
复制相似问题