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 ---
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With