python - 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.
Simple python script:
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 e.g.
---
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:
---
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. It spits out about 8 of these for various files in the /yaml directory.
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. You may want to use |-
instead of |
to trim the trailing newline.
推荐阅读
- javascript - 如何使用 PHP 和 PDO 创建更多负载
- perl - 换行符在 perl 中被视为空格
- python - 用用户输入的变量替换 .docx 文件中的变量
- vue.js - 带有 HTTP 和 HTTPS url 的 Vue JS 项目
- c# - .Net 中带有订阅的 GraphQL 客户端
- php - 微前端:具有多个前端框架的 Web 应用程序
- c++ - 以嵌套类为参数的朋友模板声明
- r - 以编程方式将值发送到过滤器的 tidyeval 方式
- node.js - 节点js上的Multer sftp文件上传问题
- amazon-web-services - 我们可以使用 AWS Spot 实例来保证挖矿利润吗?