我正在从 csv 创建 yaml 文件,其中包含很多 unicode 字符,但我似乎无法让它转储 unicode,而不会给我一个解码错误。
我正在使用ruamel.yaml
图书馆。
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 11: ordinal not in range(128)
我尝试过解析字符串、unicode 字符串、使用“utf-8”编码似乎没有任何效果。我已经看到很多示例显示添加代表来解决问题,但他们似乎都在使用旧方法来处理 ruamel,我似乎无法在任何地方记录的新方法中找到如何做到这一点。
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"))
我可以对整个 dict 进行 unicode 并且可以正常工作,但它并没有给我我需要的格式。
我需要的只是:
text: HELLO_WORLD©
我怎样才能做到这一点?