Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dump in PyYaml as utf-8

I'm trying to load a bunch of utf-8 encoded strings and dump them again with PyYaml. This is the code for loading/dumping:

lang_yml = yaml.load(codecs.open(lang + ".yml.old", "r", "utf-8")) test_file_path = lang + '.yml' stream = file(test_file_path, 'w') yaml.dump(lang_yml, stream,  default_flow_style=False, encoding=('utf-8')) 

But a strings that start as "En arrière" ends up being saved as "En arri\xE8re". What am I doing wrong?

like image 236
panmari Avatar asked May 18 '12 07:05

panmari


People also ask

What does YAML dump return?

In this case, yaml. dump will write the produced YAML document into the file. Otherwise, yaml. dump returns the produced document.

What is Safe_load in YAML?

safe_load(stream) Parses the given and returns a Python object constructed from the first document in the stream. safe_load recognizes only standard YAML tags and cannot construct an arbitrary Python object.

Is PyYAML part of Python?

PyYAML is a YAML parser and emitter for Python.


1 Answers

Found the answer myself. I just had to dump it with the argument

allow_unicode=True 

Source: http://dpinte.wordpress.com/2008/10/31/pyaml-dump-option/

like image 130
panmari Avatar answered Sep 30 '22 08:09

panmari