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


Let’s save our code in a file called

# 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


To run the script we just created we’ll do python

{'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.