简体   繁体   English

无法在 python 中解析 yaml 文件

[英]Unable to parse yaml file in python

I am trying to parse the below yaml file, tried using different yaml parsers but i'm unable to parse the yaml file.Is there way to parse this yaml file using any other python library.我正在尝试解析下面的 yaml 文件,尝试使用不同的 yaml 解析器,但我无法解析 yaml 文件。有没有办法使用任何其他 python 库来解析这个 yaml 文件。

policies:
  - name: test
    resource: ec2
    mode:
      type: periodic
      schedule: rate(1 day)
      role: arn:aws:iam::{account_id}:role/role_name
      tags:
        Name: ${tag_name}
        Project: ${tag_project}
    filters:
      - and:
        - type: value
          key: testname
          value: "${aws_account_id}"
        - type: value
          whitelist: %{ for acct_id in split (",", resource_list) }
            - "${acct_id}" %{ endfor }

python script:蟒蛇脚本:

import glob
from pathlib import Path
import ruamel.yaml

def print_table(filepath):
 file_name = Path(filepath)
 yaml = ruamel.yaml.YAML()
 data = yaml.load(file_name)
 name = data['name']
 print(name)

if __name__ == "__main__":
    print_table('test.yaml')
    

Error:错误:

ruamel.yaml.scanner.ScannerError: while scanning for the next token
found character '%' that cannot start any token
  in "test.yaml", line 17, column 26

Just use ${{each in }} .只需使用${{each in }}

policies:
  - name: test
    resource: ec2
    mode:
      type: periodic
      schedule: rate(1 day)
      role: arn:aws:iam::{account_id}:role/role_name
      tags:
        Name: ${tag_name}
        Project: ${tag_project}
    filters:
      - and:
        - type: value
          key: testname
          value: "${aws_account_id}"
        - type: value
          whitelist:
              ${{each acct_id in split (",", resource_list)}} - ${{acct_id}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM