简体   繁体   English

Pyyaml 无法解析包含缩进块的 yaml 文件(使用 | 或 >)

[英]Pyyaml unable to parse a yaml file containing an indented block (using | or >)

I'm trying to write a python script to parse a yaml file that contains markdown contained in a block literal.我正在尝试编写 python 脚本来解析包含 markdown 的 yaml 文件,该文件包含在块文字中。

Simple python script:简单的 python 脚本:

import yaml

file = open('single-source.yaml')

documentation = yaml.load(file, Loader=yaml.FullLoader)

print(documentation)

it works great when parsing a simple Yaml file eg它在解析简单的 Yaml 文件时效果很好,例如

---
product name: Azure big VM
product version: 1.0.0
Operating System: RHEL
disk type: premium ssd
location: uk-south

However, when I try to do it with a YAML file using a pipe to escape a block of markdown (as is needed for my usecase), such as:但是,当我尝试使用 YAML 文件执行此操作时,使用 pipe 转义 markdown 块(根据我的用例需要),例如:

---
product name: | Azure big VM
product version: 1.0.0
Operating System: RHEL
disk type: premium ssd
location: uk-south

It starts to throw a ton of trackback errors pointing at the pyyaml files in python directory, as shown below.它开始抛出大量指向 python 目录中的 pyyaml 文件的引用错误,如下所示。 It spits out about 8 of these for various files in the /yaml directory.它为 /yaml 目录中的各种文件吐出了其中的大约 8 个。

 File "script.py", line 5, in <module>
    documentation = yaml.load(file, Loader=yaml.FullLoader)   File "C:\Users\Ryan\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\yaml\__init__.py", line 114, in load
    return loader.get_single_data()

Can anybody offer any advice or work arounds?任何人都可以提供任何建议或解决方法吗?

Well as you said yourself, the block scalar must be indented .正如您自己所说,块标量必须缩进 So this is the correct syntax:所以这是正确的语法:

---
product name: |
  Azure big VM
product version: 1.0.0
Operating System: RHEL
disk type: premium ssd
location: uk-south

You can't have content in the header. header 中不能有内容。 You may want to use |- instead of |您可能想使用|-而不是| to trim the trailing newline.修剪尾随换行符。

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

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