使用YAML转储unicode [英] Dumping unicode with YAML
问题描述
我正在从csv的文件中创建包含很多unicode字符的yaml文件,但是如果没有给我一个解码错误,我似乎无法得到它来转储unicode.
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.
我正在使用ruamel.yaml
库.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 11: ordinal not in range(128)
我尝试解析字符串,unicode字符串,使用"utf-8"编码似乎无济于事.我已经看到了很多示例,这些示例显示了添加代表以解决问题,但是它们似乎都使用了ruamel的旧方法,而且我似乎无法找到在任何地方都有记载的较新方法中如何做到这一点./p>
I've tried parsing strings, unicode strings, encoding with "utf-8" nothing seems to work. I've seen a lot of examples that show adding a representer to solve the issue but they all seem to be using the old method for ruamel and I can't seem to find out how to do that in the newer method documented anywhere.
from ruamel.yaml import YAML
class YamlObject(YAML):
def __init__(self):
YAML.__init__(self)
self.default_flow_style = False
self.block_seq_indent = 2
self.indent = 4
self.allow_unicode = True
textDict = {"text": u"HELLO_WORLD©"}
textFile = "D:\\testFile.yml"
yaml = YamlObject()
yaml.dump(textDict, file(textFile, "w"))
我可以对整个字典进行统一编码,并且可以正常工作,但是并不能提供我需要返回的格式.
I can unicode the entire dict and that works but it doesn't give me the format I need back.
我所需要的只是:
text: HELLO_WORLD©
我该怎么做?
推荐答案
您在派生的YAML
对象中缺少encoding
.
You're missing encoding
in your derived YAML
object.
尝试这样:
class YamlObject(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'
如果您查看基类YAML
的定义,您会发现默认情况下encoding
是未定义的:
self.encoding = None
,并通过YAML.dump()
和YAML.dump_all()
.在全局
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).
更新.这实际上是Python 2 ruamel.yaml
中的一个错误(感谢@Anthon).
Update. This actually is a bug in ruamel.yaml
for Python 2 (Thanks @Anthon).
这篇关于使用YAML转储unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!