简体   繁体   中英

How would I parse 'Front Matter' with Python

I can't seem any examples of how to parse 'Front Matter' with Python. I have the following:

---
name: David
password: dwewwsadas
email: david@domain.com
websiteName: Website Name
websitePrefix: websiteprefix
websiteDomain: domain.com
action: create
---

and I am using the following code:

listing = os.listdir(path)
for infile in listing:
    stream = open(os.path.join(path, infile), 'r')
    docs = yaml.load_all(stream)
    for doc in docs:
        for k,v in doc.items():
            print k, "->", v
    print "\n",

I keep getting errors because of the second set of ---

I know it is an old question, but I just faced the same problem and used python-frontmatter . Here is an example to add a new variable to the Front matter :

import frontmatter
import io
from os.path import basename, splitext
import glob

# Where are the files to modify
path = "en/*.markdown"

# Loop through all files
for fname in glob.glob(path):
    with io.open(fname, 'r') as f:
        # Parse file's front matter
        post = frontmatter.load(f)
        if post.get('author') == None:
            post['author'] = "alex"
            # Save the modified file
            newfile = io.open(fname, 'w', encoding='utf8')
            frontmatter.dump(post, newfile)
            newfile.close()

Source : How to parse frontmatter with python

The --- starts new documents and that causes your second document to be empty, and doc to be None for the second part. You walk over the key, value pairs of the doc as if every doc is a Python dict or equivalent type, but None is not, so you should test for that inside your loop (there are of course multiple ways to do that, and on what to do if doc is not a dict ):

....
for doc in yaml.load_all(stream):
    if hasattr(doc, 'items'):
        for k, v in doc.items():
            print k, "->", v
    else:
        print doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM