How to convert yaml data to python data

This requires that you have PyYAML installed.

sudo pip install pyyaml

YAML file

Let’s assume your YAML file is called post.yml.

---
author: aamnah
date: 28-08-2014
slug: sample-post
tags: sample, post, example
categories: code

Code

Let’s save our code in a file called convertyaml.py.

# Let's open the .yml file, read it and save it in a variable called data
data = open('post.yml').read()

# Load the YAML library and convert it
import yaml
myvars = yaml.load(data)

# Let's pretty print that data to screen
from pprint import pprint as pp
pp(myvars)

Output

To run the script we just created we’ll do python convertyaml.py.

{'author': 'aamnah',
'categories': 'code',
'date': '28-08-2014',
'layout': 'post',
'slug': 'sample-post',
'tags': 'sample, post, example'}

Note that it lists the variables in alphabetical order.