This requires that you have PyYAML installed.
1sudo pip install pyyaml
Let’s assume your YAML file is called post.yml
.
1---
2author: aamnah
3date: 28-08-2014
4slug: sample-post
5tags: sample, post, example
6categories: code
Let’s save our code in a file called convertyaml.py
.
1# Let's open the .yml file, read it and save it in a variable called data
2data = open('post.yml').read()
3
4# Load the YAML library and convert it
5import yaml
6myvars = yaml.load(data)
7
8# Let's pretty print that data to screen
9from pprint import pprint as pp
10pp(myvars)
To run the script we just created we’ll do python convertyaml.py
.
1{'author': 'aamnah',
2'categories': 'code',
3'date': '28-08-2014',
4'layout': 'post',
5'slug': 'sample-post',
6'tags': 'sample, post, example'}
Note that it lists the variables in alphabetical order.