1. <small id='fjmBZ'></small><noframes id='fjmBZ'>

      <tfoot id='fjmBZ'></tfoot>

      <legend id='fjmBZ'><style id='fjmBZ'><dir id='fjmBZ'><q id='fjmBZ'></q></dir></style></legend>

          <bdo id='fjmBZ'></bdo><ul id='fjmBZ'></ul>

        <i id='fjmBZ'><tr id='fjmBZ'><dt id='fjmBZ'><q id='fjmBZ'><span id='fjmBZ'><b id='fjmBZ'><form id='fjmBZ'><ins id='fjmBZ'></ins><ul id='fjmBZ'></ul><sub id='fjmBZ'></sub></form><legend id='fjmBZ'></legend><bdo id='fjmBZ'><pre id='fjmBZ'><center id='fjmBZ'></center></pre></bdo></b><th id='fjmBZ'></th></span></q></dt></tr></i><div id='fjmBZ'><tfoot id='fjmBZ'></tfoot><dl id='fjmBZ'><fieldset id='fjmBZ'></fieldset></dl></div>
      1. 是否可以使用 PyYAML 读取用“YAML front matter"编

        时间:2023-09-12
          <bdo id='FP6Dy'></bdo><ul id='FP6Dy'></ul>
          <tfoot id='FP6Dy'></tfoot>
          <legend id='FP6Dy'><style id='FP6Dy'><dir id='FP6Dy'><q id='FP6Dy'></q></dir></style></legend>

          • <small id='FP6Dy'></small><noframes id='FP6Dy'>

            <i id='FP6Dy'><tr id='FP6Dy'><dt id='FP6Dy'><q id='FP6Dy'><span id='FP6Dy'><b id='FP6Dy'><form id='FP6Dy'><ins id='FP6Dy'></ins><ul id='FP6Dy'></ul><sub id='FP6Dy'></sub></form><legend id='FP6Dy'></legend><bdo id='FP6Dy'><pre id='FP6Dy'><center id='FP6Dy'></center></pre></bdo></b><th id='FP6Dy'></th></span></q></dt></tr></i><div id='FP6Dy'><tfoot id='FP6Dy'></tfoot><dl id='FP6Dy'><fieldset id='FP6Dy'></fieldset></dl></div>

                  <tbody id='FP6Dy'></tbody>

                1. 本文介绍了是否可以使用 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"编写的文本文件?堵在里面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                    <legend id='QF5fd'><style id='QF5fd'><dir id='QF5fd'><q id='QF5fd'></q></dir></style></legend>
                    <i id='QF5fd'><tr id='QF5fd'><dt id='QF5fd'><q id='QF5fd'><span id='QF5fd'><b id='QF5fd'><form id='QF5fd'><ins id='QF5fd'></ins><ul id='QF5fd'></ul><sub id='QF5fd'></sub></form><legend id='QF5fd'></legend><bdo id='QF5fd'><pre id='QF5fd'><center id='QF5fd'></center></pre></bdo></b><th id='QF5fd'></th></span></q></dt></tr></i><div id='QF5fd'><tfoot id='QF5fd'></tfoot><dl id='QF5fd'><fieldset id='QF5fd'></fieldset></dl></div>

                      <tfoot id='QF5fd'></tfoot>
                      • <bdo id='QF5fd'></bdo><ul id='QF5fd'></ul>

                    1. <small id='QF5fd'></small><noframes id='QF5fd'>