Skip to content Skip to sidebar Skip to footer

Dumping Unicode With Yaml

I'm creating yaml files from csv's that have a lot of unicode characters in them but I can't seem to get it to dump the unicode without it giving me a Decode Error. I'm using the r

Solution 1:

You're missing encoding in your derived YAML object.

Try like this:

classYamlObject(YAML):
    def__init__(self):
        YAML.__init__(self)
        self.default_flow_style = False
        self.block_seq_indent = 2
        self.indent = 4
        self.allow_unicode = True
        self.encoding = 'utf-8'

If you look at the definition of your base class, YAML, you'll notice that by default, encoding is undefined:

self.encoding = None

and it stays None through YAML.dump() and YAML.dump_all(). In the global dump() method, on contrary, encoding is set to a default utf-8 (in Python 2 only).

Update. This actually is a bug in ruamel.yaml for Python 2 (Thanks @Anthon).

Post a Comment for "Dumping Unicode With Yaml"