Is it possible to use PyYAML to read a text file written with a “YAML front matter” block inside?

前端 未结 2 1653
-上瘾入骨i
-上瘾入骨i 2020-12-12 02:56

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

相关标签:
2条回答
  • 2020-12-12 02:59

    You can accomplish this without any custom parsing by calling yaml.load_all() instead. This will return a generator of which the first item is the expected front matter as a dict, and the second is the rest of the document as a string:

    import yaml
    
    with open('some-file-with-front-matter.md') as f:
        front_matter, content = list(yaml.load_all(f, Loader=yaml.FullLoader))[:2]
    

    If you just want the front matter it's even simpler:

    import yaml
    
    with open('some-file-with-front-matter.md') as f:
        front_matter = next(yaml.load_all(f, Loader=yaml.FullLoader))
    

    This works because yaml.load_all() is for loading several YAML documents within the same document, delimited by ---. Also, make sure you take the usual precautions when loading YAML from an unknown source.

    EDIT: Updated the code to include a Loader argument which is required now, and updated the documentation link. Also verified the code works even with --- in the content, per comment below.

    0 讨论(0)
  • 2020-12-12 03:20

    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() != '---\n':
        f.seek(pointer)
        return ''
      readline = iter(f.readline, '')
      readline = iter(readline.next, '---\n')
      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
    
    0 讨论(0)
提交回复
热议问题