How to force PyYAML to load strings as unicode objects?

Posted by Petr Viktorin on Stack Overflow See other posts from Stack Overflow or by Petr Viktorin
Published on 2010-05-22T23:27:31Z Indexed on 2010/05/22 23:30 UTC
Read the original article Hit count: 347

Filed under:
|

The PyYAML package loads unmarked strings as either unicode or str objects, depending on their content.

I would like to use unicode objects throughout my program (and, unfortunately, can't switch to Python 3 just yet).

Is there an easy way to force PyYAML to always strings load unicode objects? I do not want to clutter my YAML with !!python/unicode tags.

# Encoding: UTF-8

import yaml

menu= u"""---
- spam
- eggs
- bacon
- crème brûlée
- spam
"""

print yaml.load(menu)

Output: ['spam', 'eggs', 'bacon', u'cr\xe8me br\xfbl\xe9e', 'spam']

I would like: [u'spam', u'eggs', u'bacon', u'cr\xe8me br\xfbl\xe9e', u'spam']

© Stack Overflow or respective owner

Related posts about python-2.x

Related posts about pyyaml