tags:

views:

414

answers:

3

What is the best way to parse a YAML file in Python?

+5  A: 

Check out http://pyyaml.org/

BStruthers
A: 

Wikipedia links at least PyYAML and PySyck. The former being a pure Python library.

Joey
+6  A: 

The easiest and pureist method without relying on C headers is PyYaml:

#!/usr/bin/env python

import yaml

stream = file("example.yaml", 'r')
print yaml.load(stream)

Err.. that's it... how many lines of code would that take me in Java... any ideas? : ) more info here:

http://pyyaml.org/wiki/PyYAMLDocumentation

Jon
+1 Recommendation, code sample, and documentation link.
hughdbrown
The use of `file()` is deprecated; in Python 3.x it has been removed. The use of `open()` works on all versions of Python.
steveha
in Java it is also a couple of lines:http://code.google.com/p/snakeyaml/wiki/readme#Documentation
A couple is 2... that's 5 lines of code... :)
Jon