在python中读取以``结尾和开头的YAML文件

2024-10-04 07:24:47 发布

您现在位置:Python中文网/ 问答频道 /正文

stream = open(afile, 'r')
self.meta = yaml.load(stream)

在python中可以很容易地读取YAML文件,但是在I reach错误末尾它没有得到---(与...相同):

^{pr2}$

但是YAML规范allow说:

YAML uses three dashes (“---”) to separate directives from document content. This also serves to signal the start of a document if no directives are present. Three dots ( “...”) indicate the end of a document without starting a new one, for use in communication channels.

你怎么看这个

---
title: "El punt de llibre"
abstract: "Estimar a quina pàgina està el punt de llibre"
keywords: ["when", "activitat", "3/3", "grup", "estimació", "aproximació", "funció lineal - proporcionalitat", "ca"]
comments: true
...

在python中?在


Tags: ofthetoselfyamlstreamdeopen
2条回答

如果您的YAML源文件包含多个文档,则可以使用

list(yaml.safe_load_all(stream))[0]

然而,奇怪的是,...会导致PyYaml中断,您可能需要将其报告为bug。在

您的YAML流/文件中似乎有多个文档,例如,尝试解析此文件时,会出现相同的错误消息:

 -
title: "El punt de llibre"
abstract: "Estimar a quina pàgina està el punt de llibre"
keywords: ["when", "activitat", "3/3", "grup", "estimació", "aproximació", "funció lineal - proporcionalitat", "ca"]
comments: true
...
 -
title: "El punt de llibre"
abstract: "Estimar a quina pàgina està el punt de llibre"
keywords: ["when", "activitat", "3/3", "grup", "estimació", "aproximació", "funció lineal - proporcionalitat", "ca"]
comments: true
...
 -
title: "El punt de llibre"
abstract: "Estimar a quina pàgina està el punt de llibre"
keywords: ["when", "activitat", "3/3", "grup", "estimació", "aproximació", "funció lineal - proporcionalitat", "ca"]
comments: true
...

要处理这种流,可以使用以下方法:

^{pr2}$

这将显示以下内容:

^{3}$

相关问题 更多 >