简体   繁体   English

在PyYaml中转储为utf-8

[英]Dump in PyYaml as utf-8

I'm trying to load a bunch of utf-8 encoded strings and dump them again with PyYaml. 我正在尝试加载一堆utf-8编码的字符串并使用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". 但是以“恩到达”开头的字符串最终被保存为“恩到达\\ xE8re”。 What am I doing wrong? 我究竟做错了什么?

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/ 资料来源: http//dpinte.wordpress.com/2008/10/31/pyaml-dump-option/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM