4

When dumping (ruamel.yaml, PyYAML) the dict data = {'abc': 'def'} as YAML (with default_flow_style=False) in Python 2.7 you will get:

abc: def

which is fine. However if you make all strings unicode (by u prefixing or by using from __future__ import unicode_literals) this gets dumped as:

!!python/unicode 'abc': !!python/unicode 'def'

How can I dump all strings (unicode prefixed or not) without tag, without reverting to using safe_dump()? Adding allow_unicode=True doesn't do the trick.

Complete example that generates the unwanted tags:

from __future__ import unicode_literals

import sys
import ruamel.yaml

data = {'abc': 'def'}
ruamel.yaml.safe_dump(data, sys.stdout, allow_unicode=True, default_flow_style=False)
4

1 回答 1

4

您需要一个不同的代表来处理unicode转换str

from __future__ import unicode_literals

import sys
import ruamel.yaml

def my_unicode_repr(self, data):
    return self.represent_str(data.encode('utf-8'))

ruamel.yaml.representer.Representer.add_representer(unicode, my_unicode_repr)

data = {'abc': u'def'}
ruamel.yaml.dump(data, sys.stdout, allow_unicode=True, default_flow_style=False)

给出:

abc: def

对于 PyYAML,这也可以,只需替换ruamel.yamlyaml

于 2016-09-21T12:23:59.593 回答