微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从 Python 字典中获取每个 URL?

如何解决如何从 Python 字典中获取每个 URL?

我有一个代表菜单的 yaml 字符串。在我从中获得字典后,我尝试获取 URL 只是说 // Access the earlier property in this form anywhere // Below is an example public partial class Form2 : Form { public Form2() { InitializeComponent(); Form1 f1 = new Form1(); label1.Text = f1.UserName; } } 的键 url 以及不是 Parameter 的其他元素的列表的每个第一个元素。

这是我试过的

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) 下的 url(例如 Parameter)的父 URL 做得很好,但不能对 child 和 sub-child 处理。

我只想要 url 的最终结果作为这个列表

[MultiJournal,ForeignExchange]

我尝试了几种方法,但仍然无法得到结果?

有什么办法可以得到这样的价值清单吗?谢谢

解决方法

虽然我坚信您的 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"
                        },{
                      "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

字典是一组键/值对,它们都代表父键的属性:

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","ForeignExchange"
]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。