Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I parse 'Front Matter' with Python

Tags:

python

yaml

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

---
name: David
password: dwewwsadas
email: [email protected]
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 ---

like image 223
TheTemplateBlog Avatar asked Sep 06 '14 06:09

TheTemplateBlog


2 Answers

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

like image 90
Alejandro Alcalde Avatar answered Sep 28 '22 05:09

Alejandro Alcalde


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
like image 43
Anthon Avatar answered Sep 28 '22 06:09

Anthon