如何从 Python 中的字典中获取每个 URL?
How can I get every URL from dictionary in Python?
我有一个表示菜单的 yaml 字符串。在我从中得到一本字典后,我尝试获取 URL 简单地说 Parameter
的键 url
和列表中不是 Parameter
的其他元素的每个第一个元素。
这是我尝试过的
import yaml
import jmespath
import pprint
setting = """
GeneralSetting:
- Description: General Setting
MenuSetting:
- Description: Menu Setting
- Setting:
- AutoMenu:
- Description: Register menu
- HelpText: This is for auto create menu
- Value:
- Sample:
- Parameter:
- icon: fa fa-birthday-cake
- Active: Y
- Sample: [Sample]
- Sample Account: [SampleAccount]
- Multi Journal:
- Parameter:
- icon: fas fa-book
- url: MultiJournal
- Foreign Exchange:
- Parameter:
- icon: fa fa-money-bill-alt
- url: ForeignExchange
- Loan Contract:
- Parameter:
- icon: fa fa-capsules
- Active: Y
- Loan Contract: [LoanContract,1]
- Loan Report:
- Loan Detail Listing: [LoanDetailListing,1]
- Loan Application Detail Listing: [ReportR003,1]
"""
toDict = yaml.load(setting, yaml.SafeLoader)
pp = pprint.PrettyPrinter(indent=2)
# ALL
ALL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[]', toDict)
ParentURL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[0][].Parameter[].url', toDict)
NotParameter = {key:value for item in ALL for it in item for key,value in it.items() if key != "Parameter"}
# pp.pprint(NotParameter)
# print ('-----')
SubURL = jmespath.search('*[0]', NotParameter)
SubSubURL = jmespath.search('*[].*[][]', NotParameter)
pp.pprint(ParentURL)
print('---')
pp.pprint(SubURL)
print('---')
pp.pprint(SubSubURL)
据此,我只能对 Parameter
下的 url
Parent URL 做的很好,例如 [MultiJournal,ForeignExchange]
但对 child 和 sub-child.
我只想要url的最终结果作为这个
的列表
[Sample,SampleAccount,MultiJournal,ForeignExchange,LoanContract,LoanDetailListing,Report/R003]
我尝试了几种方法,但仍然无法得到结果?
有什么方法可以让我得到这样的价值清单?谢谢
虽然我坚信您的 YAML 结构不是您应该拥有的结构(更多内容在下方),但这里将是一个与您的预期输出相匹配的查询:
MenuSetting[].Setting[].AutoMenu[2].[Value[0].[Sample[].Sample, Sample[]."Sample Account"], Value[]."Multi Journal"[].Parameter[1].url, Value[]."Foreign Exchange"[].Parameter[1].url, Value[]."Loan Contract"[]."Loan Contract"[0], Value[]."Loan Contract"[]."Loan Report"[0]."Loan Detail Listing"[0], Value[]."Loan Contract"[]."Loan Report"[1]."Loan Application Detail Listing"[0]][][][][]
基于 YAML 的 JSON 等价物:
{
"GeneralSetting": [
{
"Description": "General Setting"
}
],
"MenuSetting": [
{
"Description": "Menu Setting"
},
{
"Setting": [
{
"AutoMenu": [
{
"Description": "Register menu"
},
{
"HelpText": "This is for auto create menu"
},
{
"Value": [
{
"Sample": [
{
"Parameter": [
{
"icon": "fa fa-birthday-cake"
},
{
"Active": "Y"
}
]
},
{
"Sample": [
"Sample"
]
},
{
"Sample Account": [
"SampleAccount"
]
}
]
},
{
"Multi Journal": [
{
"Parameter": [
{
"icon": "fas fa-book"
},
{
"url": "MultiJournal"
}
]
}
]
},
{
"Foreign Exchange": [
{
"Parameter": [
{
"icon": "fa fa-money-bill-alt"
},
{
"url": "ForeignExchange"
}
]
}
]
},
{
"Loan Contract": [
{
"Parameter": [
{
"icon": "fa fa-capsules"
},
{
"Active": "Y"
}
]
},
{
"Loan Contract": [
"LoanContract",
1
]
},
{
"Loan Report": [
{
"Loan Detail Listing": [
"LoanDetailListing",
1
]
},
{
"Loan Application Detail Listing": [
"ReportR003",
1
]
}
]
}
]
}
]
}
]
}
]
}
]
}
此查询给出:
[
"Sample",
"SampleAccount",
"MultiJournal",
"ForeignExchange",
"LoanContract",
"LoanDetailListing",
"ReportR003"
]
现在我认为你的 YAML 有很多问题,你混淆了 YAML 中的列表和字典。
列表将以破折号开头:
- carrot
- onions
- potatoes
字典是一组 key/value 对,它们都表示父键的属性:
car:
brand: ferrari
model: 488
engine: "3.9 l twin-turbocharged"
当然,你可以有一个字典列表:
- name: duck
sound: quack
legs: 2
- name: cow
sound: moo
legs: 4
所以在你的 YAML 中,如果我关注它的顶部,我相信它应该是这样的:
GeneralSetting:
Description: General Setting
MenuSetting:
Description: Menu Setting
Setting:
AutoMenu:
Description: Register menu
HelpText: This is for auto create menu
Value:
Sample:
Parameter:
icon: fa fa-birthday-cake
Active: Y
Sample: Sample
Sample Account: SampleAccount
Multi Journal:
Parameter:
icon: fas fa-book
url: MultiJournal
Foreign Exchange:
Parameter:
icon: fa fa-money-bill-alt
url: ForeignExchange
# ...
当然,这会完全改变和简化查询,例如,基于此 YAML,结果 JSON 将是:
{
"GeneralSetting": {
"Description": "General Setting"
},
"MenuSetting": {
"Description": "Menu Setting",
"Setting": {
"AutoMenu": {
"Description": "Register menu",
"HelpText": "This is for auto create menu",
"Value": {
"Sample": {
"Parameter": {
"icon": "fa fa-birthday-cake",
"Active": "Y"
},
"Sample": "Sample",
"Sample Account": "SampleAccount"
},
"Multi Journal": {
"Parameter": {
"icon": "fas fa-book",
"url": "MultiJournal"
}
},
"Foreign Exchange": {
"Parameter": {
"icon": "fa fa-money-bill-alt",
"url": "ForeignExchange"
}
}
}
}
}
}
}
第一个元素的查询是:
MenuSetting.Setting.AutoMenu.Value.[Sample.Sample, Sample."Sample Account", *.Parameter.url][]
这将给出预期的结果:
[
"Sample",
"SampleAccount",
"MultiJournal",
"ForeignExchange"
]
我有一个表示菜单的 yaml 字符串。在我从中得到一本字典后,我尝试获取 URL 简单地说 Parameter
的键 url
和列表中不是 Parameter
的其他元素的每个第一个元素。
这是我尝试过的
import yaml
import jmespath
import pprint
setting = """
GeneralSetting:
- Description: General Setting
MenuSetting:
- Description: Menu Setting
- Setting:
- AutoMenu:
- Description: Register menu
- HelpText: This is for auto create menu
- Value:
- Sample:
- Parameter:
- icon: fa fa-birthday-cake
- Active: Y
- Sample: [Sample]
- Sample Account: [SampleAccount]
- Multi Journal:
- Parameter:
- icon: fas fa-book
- url: MultiJournal
- Foreign Exchange:
- Parameter:
- icon: fa fa-money-bill-alt
- url: ForeignExchange
- Loan Contract:
- Parameter:
- icon: fa fa-capsules
- Active: Y
- Loan Contract: [LoanContract,1]
- Loan Report:
- Loan Detail Listing: [LoanDetailListing,1]
- Loan Application Detail Listing: [ReportR003,1]
"""
toDict = yaml.load(setting, yaml.SafeLoader)
pp = pprint.PrettyPrinter(indent=2)
# ALL
ALL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[]', toDict)
ParentURL = jmespath.search('MenuSetting[].Setting[].AutoMenu[].Value[].*[0][].Parameter[].url', toDict)
NotParameter = {key:value for item in ALL for it in item for key,value in it.items() if key != "Parameter"}
# pp.pprint(NotParameter)
# print ('-----')
SubURL = jmespath.search('*[0]', NotParameter)
SubSubURL = jmespath.search('*[].*[][]', NotParameter)
pp.pprint(ParentURL)
print('---')
pp.pprint(SubURL)
print('---')
pp.pprint(SubSubURL)
据此,我只能对 Parameter
下的 url
Parent URL 做的很好,例如 [MultiJournal,ForeignExchange]
但对 child 和 sub-child.
我只想要url的最终结果作为这个
的列表[Sample,SampleAccount,MultiJournal,ForeignExchange,LoanContract,LoanDetailListing,Report/R003]
我尝试了几种方法,但仍然无法得到结果?
有什么方法可以让我得到这样的价值清单?谢谢
虽然我坚信您的 YAML 结构不是您应该拥有的结构(更多内容在下方),但这里将是一个与您的预期输出相匹配的查询:
MenuSetting[].Setting[].AutoMenu[2].[Value[0].[Sample[].Sample, Sample[]."Sample Account"], Value[]."Multi Journal"[].Parameter[1].url, Value[]."Foreign Exchange"[].Parameter[1].url, Value[]."Loan Contract"[]."Loan Contract"[0], Value[]."Loan Contract"[]."Loan Report"[0]."Loan Detail Listing"[0], Value[]."Loan Contract"[]."Loan Report"[1]."Loan Application Detail Listing"[0]][][][][]
基于 YAML 的 JSON 等价物:
{
"GeneralSetting": [
{
"Description": "General Setting"
}
],
"MenuSetting": [
{
"Description": "Menu Setting"
},
{
"Setting": [
{
"AutoMenu": [
{
"Description": "Register menu"
},
{
"HelpText": "This is for auto create menu"
},
{
"Value": [
{
"Sample": [
{
"Parameter": [
{
"icon": "fa fa-birthday-cake"
},
{
"Active": "Y"
}
]
},
{
"Sample": [
"Sample"
]
},
{
"Sample Account": [
"SampleAccount"
]
}
]
},
{
"Multi Journal": [
{
"Parameter": [
{
"icon": "fas fa-book"
},
{
"url": "MultiJournal"
}
]
}
]
},
{
"Foreign Exchange": [
{
"Parameter": [
{
"icon": "fa fa-money-bill-alt"
},
{
"url": "ForeignExchange"
}
]
}
]
},
{
"Loan Contract": [
{
"Parameter": [
{
"icon": "fa fa-capsules"
},
{
"Active": "Y"
}
]
},
{
"Loan Contract": [
"LoanContract",
1
]
},
{
"Loan Report": [
{
"Loan Detail Listing": [
"LoanDetailListing",
1
]
},
{
"Loan Application Detail Listing": [
"ReportR003",
1
]
}
]
}
]
}
]
}
]
}
]
}
]
}
此查询给出:
[
"Sample",
"SampleAccount",
"MultiJournal",
"ForeignExchange",
"LoanContract",
"LoanDetailListing",
"ReportR003"
]
现在我认为你的 YAML 有很多问题,你混淆了 YAML 中的列表和字典。
列表将以破折号开头:
- carrot
- onions
- potatoes
字典是一组 key/value 对,它们都表示父键的属性:
car:
brand: ferrari
model: 488
engine: "3.9 l twin-turbocharged"
当然,你可以有一个字典列表:
- name: duck
sound: quack
legs: 2
- name: cow
sound: moo
legs: 4
所以在你的 YAML 中,如果我关注它的顶部,我相信它应该是这样的:
GeneralSetting:
Description: General Setting
MenuSetting:
Description: Menu Setting
Setting:
AutoMenu:
Description: Register menu
HelpText: This is for auto create menu
Value:
Sample:
Parameter:
icon: fa fa-birthday-cake
Active: Y
Sample: Sample
Sample Account: SampleAccount
Multi Journal:
Parameter:
icon: fas fa-book
url: MultiJournal
Foreign Exchange:
Parameter:
icon: fa fa-money-bill-alt
url: ForeignExchange
# ...
当然,这会完全改变和简化查询,例如,基于此 YAML,结果 JSON 将是:
{
"GeneralSetting": {
"Description": "General Setting"
},
"MenuSetting": {
"Description": "Menu Setting",
"Setting": {
"AutoMenu": {
"Description": "Register menu",
"HelpText": "This is for auto create menu",
"Value": {
"Sample": {
"Parameter": {
"icon": "fa fa-birthday-cake",
"Active": "Y"
},
"Sample": "Sample",
"Sample Account": "SampleAccount"
},
"Multi Journal": {
"Parameter": {
"icon": "fas fa-book",
"url": "MultiJournal"
}
},
"Foreign Exchange": {
"Parameter": {
"icon": "fa fa-money-bill-alt",
"url": "ForeignExchange"
}
}
}
}
}
}
}
第一个元素的查询是:
MenuSetting.Setting.AutoMenu.Value.[Sample.Sample, Sample."Sample Account", *.Parameter.url][]
这将给出预期的结果:
[
"Sample",
"SampleAccount",
"MultiJournal",
"ForeignExchange"
]