是否可以使用 PyYAML 读取用“YAML front matter"编写的文本文件?堵在里面?

时间:2023-04-15
本文介绍了是否可以使用 PyYAML 读取用“YAML front matter"编写的文本文件?堵在里面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

抱歉,我对 YAML 和 PyYAML 知之甚少,但我喜欢支持以Jekyll"(http://jekyllrb.com/docs/frontmatter/) AFAIK 拥有这些对我来说看起来非常酷和性感的YAML Front Matter"块.
所以我在我的电脑上安装了 PyYAML,并用这段文本写了一个小文件:

I'm sorry, I know very little of both YAML and PyYAML but I felt in love with the idea of supporting a configuration file written in the same style used by "Jekyll" (http://jekyllrb.com/docs/frontmatter/) that AFAIK have these "YAML Front Matter" blocks that looks very cool and sexy to me.
So I installed PyYAML on my computer and I wrote a small file with this block of text:

---
First Name: John
Second Name: Doe
Born: Yes
---

Lorem ipsum dolor sit amet, consectetur adipiscing elit,  
sed do eiusmod tempor incididunt ut labore et dolore magna  
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
laboris nisi ut aliquip ex ea commodo consequat.

然后我尝试使用以下代码通过 Python 3.4 和 PyYAML 读取此文本文件:

Then I tried to read this text file with Python 3.4 and PyYAML by using this code:

import yaml

stream = open("test.yaml")
a = stream.read()
b = yaml.load(a)

但显然它不起作用,Python 显示此错误消息:

But obviously it's not working, and Python displays this error message:

Traceback (most recent call last):
  File "<pyshell#62>", line 1, in <module>
    b = yaml.load(a)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/yaml/__init__.py", line 72, in load
    return loader.get_single_data()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/yaml/constructor.py", line 35, in get_single_data
    node = self.get_single_node()
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/yaml/composer.py", line 43, in get_single_node
    event.start_mark)
yaml.composer.ComposerError: expected a single document in the stream
  in "<unicode string>", line 2, column 1:
    First Name: John
    ^
but found another document
  in "<unicode string>", line 5, column 1:
    ---
    ^

你能帮帮我吗?
我是否以错误的方式编写了代码,或者这是否意味着 PyYAML 无法处理 YAML 前端块?
还有什么我可以尝试用 PyYAML 做的,还是我必须使用正则表达式编写自己的解析器?

Could you help me, please?
Have I wrote the code in the wrong way, or does this means that PyYAML can't handle YAML front matter blocks?
Is there anything else I could try to do with PyYAML, or do I have to write my own parser by using regex ?

非常感谢您的宝贵时间!

Thank you very much for your time !

推荐答案

Python yaml 库不支持读取文档中嵌入的 yaml.这是一个提取 yaml 文本的实用程序函数,因此您可以在读取文件的其余部分之前对其进行解析:

The Python yaml library does not support reading yaml that is embedded in a document. Here is a utility function that extracts the yaml text, so you can parse it before reading the remainder of the file:

#!/usr/bin/python2.7

import yaml
import sys

def get_yaml(f):
  pointer = f.tell()
  if f.readline() != '---
':
    f.seek(pointer)
    return ''
  readline = iter(f.readline, '')
  readline = iter(readline.next, '---
')
  return ''.join(readline)


for filename in sys.argv[1:]:
  with open(filename) as f:
    config = yaml.load(get_yaml(f))
    text = f.read()
    print "TEXT from", filename
    print text
    print "CONFIG from", filename
    print config

这篇关于是否可以使用 PyYAML 读取用“YAML front matter"编写的文本文件?堵在里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:为什么 PyYAML 使用生成器来构造对象? 下一篇:为什么json序列化比Python中的yaml序列化快那么多?

相关文章