pyyaml:无标签倾销 [英] pyyaml: dumping without tags

查看:24
本文介绍了pyyaml:无标签倾销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

<预><代码>>>>导入 yaml>>>yaml.dump(u'abc')"!!python/unicode 'abc'\n"

但是我想要

<预><代码>>>>导入 yaml>>>yaml.dump(u'abc', magic='something')'ABC\n'

什么神奇参数强制不加标签?

解决方案

您可以使用 safe_dump 而不是 dump.请记住,那时它将无法表示任意 Python 对象.此外,当您加载 YAML 时,您将获得一个str 对象而不是unicode.

I have

>>> import yaml
>>> yaml.dump(u'abc')
"!!python/unicode 'abc'\n"

But I want

>>> import yaml
>>> yaml.dump(u'abc', magic='something')
'abc\n'

What magic param forces no tagging?

解决方案

You can use safe_dump instead of dump. Just keep in mind that it won't be able to represent arbitrary Python objects then. Also, when you load the YAML, you will get a str object instead of unicode.

这篇关于pyyaml:无标签倾销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆