Я не уверен, какие версии вы используете, но я не могу воспроизвести это. Только libyaml
, кажется, имеет проблемы с вашим вводом:
from __future__ import print_function
import sys
print('Python: ', sys.version_info[:2])
import yaml as pyyaml
print('PyYAML: ', pyyaml.__version__)
import ruamel.yaml
print('ruamel.yaml:', ruamel.yaml.version_info)
yaml_str = 'x: "\\uD800\\uDC00"'
print('yaml_str', yaml_str)
print(repr(pyyaml.safe_load(yaml_str)['x']))
for typ, pure in [('rt', True), ('safe', True), ('safe', False)]:
yaml = ruamel.yaml.YAML(typ=typ, pure=pure)
data = yaml.load(yaml_str)
print(repr(data['x']))
дает:
Python: (2, 7)
PyYAML: 5.1.2
ruamel.yaml: (0, 16, 6, u'dev')
yaml_str x: "\uD800\uDC00"
u'\ud800\udc00'
u'\ud800\udc00'
u'\ud800\udc00'
Traceback (most recent call last):
File "/tmp/ryd-of-anthon/ryd-111/tmp_0.py", line 20, in <module>
data = yaml.load(yaml_str)
File "/home/anthon/.venv/27/lib/python2.7/site-packages/ruamel/yaml/main.py", line 341, in load
return constructor.get_single_data()
File "/home/anthon/.venv/27/lib/python2.7/site-packages/ruamel/yaml/constructor.py", line 111, in get_single_data
node = self.composer.get_single_node()
File "_ruamel_yaml.pyx", line 706, in _ruamel_yaml.CParser.get_single_node
File "_ruamel_yaml.pyx", line 724, in _ruamel_yaml.CParser._compose_document
File "_ruamel_yaml.pyx", line 775, in _ruamel_yaml.CParser._compose_node
File "_ruamel_yaml.pyx", line 889, in _ruamel_yaml.CParser._compose_mapping_node
File "_ruamel_yaml.pyx", line 731, in _ruamel_yaml.CParser._compose_node
File "_ruamel_yaml.pyx", line 904, in _ruamel_yaml.CParser._parse_next_event
ruamel.yaml.scanner.ScannerError: while parsing a quoted scalar
in "<byte string>", line 1, column 4
found invalid Unicode character escape code
in "<byte string>", line 1, column 7