YAML python解析器

时间:2016-03-12 16:25:10

标签: python dictionary yaml

我有以下格式的YAML文件:

innings:
      - 1st innings:
          team: England
          deliveries:
            - 0.1:
                batsman: ME Trescothick
                bowler: Shoaib Akhtar
                extras:
                  wides: 1
                non_striker: AJ Strauss
                runs:
                  batsman: 0
                  extras: 1
                  total: 1
            - 0.2:
                batsman: ME Trescothick
                bowler: Shoaib Akhtar
                non_striker: AJ Strauss
                runs:
                  batsman: 0
                  extras: 0
                  total: 0
      - 2nd innings:
          team: Pakistan
          deliveries:
            - 0.1:
                batsman: Shoaib Malik
                bowler: D Gough
                non_striker: Mohammad Hafeez
                runs:
                  batsman: 2
                  extras: 0
                  total: 2
            - 0.2:
                batsman: Shoaib Malik
                bowler: D Gough
                extras:
                  wides: 5
                non_striker: Mohammad Hafeez
                runs:
                  batsman: 0
                  extras: 5
                  total: 5

我使用以下代码访问yaml数据:

with open(fpath, 'r') as stream:
    datamap = yaml.safe_load(stream)
x = bunchify(datamap)
print x.innings[0]

print语句给出了以下结果:

- 1st innings:
          team: England
          deliveries:
            - 0.1:
                batsman: ME Trescothick
                bowler: Shoaib Akhtar
                extras:
                  wides: 1
                non_striker: AJ Strauss
                runs:
                  batsman: 0
                  extras: 1
                  total: 1
            - 0.2:
                batsman: ME Trescothick
                bowler: Shoaib Akhtar
                non_striker: AJ Strauss
                runs:
                  batsman: 0
                  extras: 0
                  total: 0

但是当我尝试使用声明访问第一局时:

print x.innings[0].1st innings

它会抛出错误。我甚至尝试将字符串“1st innings”分配给变量并使用它。但它引发了一个错误:

inn = "1st innings"
print x.innings[0].inn

我需要获得总运行次数。为此,我需要在列表[0.1, 0.2,...]中添加每个球的总数。

2 个答案:

答案 0 :(得分:2)

Converting to Python lists & dictionaries,你得到这个结构,称之为x

评论中的一些值是

  • x.inningsx['innings'] - 返回由"1st innings""2nd innings"
  • 键入的Python词典列表
  • x.innings[0] - 返回上面列表中的第一个字典
  • x.innings[0]['1st innings'] - 返回"1st innings"列表中第一项中innings项的字典值。
  • x.innings[0]['1st innings']['deliveries'] - 返回由0.10.2键入的Python词典列表。
  • x.innings[0]['1st innings']['deliveries'][0] - 返回上面列表中的第一个字典
  • x.innings[0]['1st innings']['deliveries'][0][0.1] - 返回0.1列表中第一项中deliveries项的JSON对象值。
  • x.innings[0]['1st innings']['deliveries'][0][0.1]['runs'] - 返回'runs'
  • 的字典

数据:

{
  "innings": [
    {
      "1st innings": {
        "deliveries": [
          {
            0.1: {
              "batsman": "ME Trescothick", 
              "bowler": "Shoaib Akhtar", 
              "runs": {
                "batsman": 0, 
                "total": 1, 
                "extras": 1
              }, 
              "extras": {
                "wides": 1
              }, 
              "non_striker": "AJ Strauss"
            }
          }, 
          {
            0.2: {
              "batsman": "ME Trescothick", 
              "bowler": "Shoaib Akhtar", 
              "runs": {
                "batsman": 0, 
                "total": 0, 
                "extras": 0
              }, 
              "non_striker": "AJ Strauss"
            }
          }
        ], 
        "team": "England"
      }
    }, 
    {
      "2nd innings": { ... }
    }
  ]
}

答案 1 :(得分:1)

@laxmi23欢迎来到Stack Overflow。请阅读How to ask a good question部分,提出可获得更多答案和更多投票的问题。您可能也喜欢阅读How to create a mimimal, complete and verifiable example

您的问题可能被拒绝了,因为似乎您在发布之前可能没有做过充分的研究。特别是YAML Tutorial on load的部分表示它返回一个Python对象。搜索Python Tutorial on Data Types,您可以看到如何索引各种Python对象,例如ListsDictionaries,这是您需要的:

>>> import yaml  # use the PyYAML package to load your YAML string
>>> yaml.load("""
innings:
- 1st innings:
    deliveries:
    - 0.1:
        batsman: ME Trescothick
        bowler: Shoaib Akhtar
        extras: {wides: 1}
        non_striker: AJ Strauss
        runs: {batsman: 0, extras: 1, total: 1}
    - 0.2:
        batsman: ME Trescothick
        bowler: Shoaib Akhtar
        non_striker: AJ Strauss
        runs: {batsman: 0, extras: 0, total: 0}
    team: England
- 2nd innings:
    deliveries:
    - 0.1:
        batsman: Shoaib Malik
        bowler: D Gough
        non_striker: Mohammad Hafeez
        runs: {batsman: 2, extras: 0, total: 2}
    - 0.2:
        batsman: Shoaib Malik
        bowler: D Gough
        extras: {wides: 5}
        non_striker: Mohammad Hafeez
        runs: {batsman: 0, extras: 5, total: 5}
    team: Pakistan
""")

{'innings': [{'1st innings': {'deliveries': [{0.1: {'batsman': 'ME Trescothick',
       'bowler': 'Shoaib Akhtar',
       'extras': {'wides': 1},
       'non_striker': 'AJ Strauss',
       'runs': {'batsman': 0, 'extras': 1, 'total': 1}}},
     {0.2: {'batsman': 'ME Trescothick',
       'bowler': 'Shoaib Akhtar',
       'non_striker': 'AJ Strauss',
       'runs': {'batsman': 0, 'extras': 0, 'total': 0}}}],
    'team': 'England'}},
  {'2nd innings': {'deliveries': [{0.1: {'batsman': 'Shoaib Malik',
       'bowler': 'D Gough',
       'non_striker': 'Mohammad Hafeez',
       'runs': {'batsman': 2, 'extras': 0, 'total': 2}}},
     {0.2: {'batsman': 'Shoaib Malik',
       'bowler': 'D Gough',
       'extras': {'wides': 5},
       'non_striker': 'Mohammad Hafeez',
       'runs': {'batsman': 0, 'extras': 5, 'total': 5}}}],
    'team': 'Pakistan'}}]}

查看响应{'innings': [{'1st innings': ...的第一行,您可以看到外部对象是一个字典,其中包含一个字典列表。因此,访问Python对象时,Python数据类型教程演示使用括号。

>>> y['innings'][0]['1st innings']  # index into the returned Python object

{'deliveries': [{0.1: {'batsman': 'ME Trescothick',
    'bowler': 'Shoaib Akhtar',
    'extras': {'wides': 1},
    'non_striker': 'AJ Strauss',
    'runs': {'batsman': 0, 'extras': 1, 'total': 1}}},
  {0.2: {'batsman': 'ME Trescothick',
    'bowler': 'Shoaib Akhtar',
    'non_striker': 'AJ Strauss',
    'runs': {'batsman': 0, 'extras': 0, 'total': 0}}}],
 'team': 'England'}

正如我在上面的评论中所说,Python字典键可以是任何hashable,甚至是float。因此,为了找到runs使用0.1作为数字而不是字符串。假设我们将load响应捕获为x

>>> x['innings'][0]['1st innings']['deliveries'][0][0.1]['runs']
{'batsman': 0, 'extras': 1, 'total': 1}