pyyaml:不带标签的转储

发布于 2024-08-16 08:44:32 字数 262 浏览 4 评论 0 原文

我有

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

但我想要

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

卸妝后依然美 2024-08-23 08:44:32

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

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.

魂归处 2024-08-23 08:44:32

怎么样:

def unicode_representer(dumper, uni):
    node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni)
    return node

yaml.add_representer(unicode, unicode_representer)

这似乎使转储 unicode 对象的工作方式与转储 str 对象对我来说相同(Python 2.6)。

In [72]: yaml.dump(u'abc')
Out[72]: 'abc\n...\n'

In [73]: yaml.dump('abc')
Out[73]: 'abc\n...\n'

In [75]: yaml.dump(['abc'])
Out[75]: '[abc]\n'

In [76]: yaml.dump([u'abc'])
Out[76]: '[abc]\n'

How about this:

def unicode_representer(dumper, uni):
    node = yaml.ScalarNode(tag=u'tag:yaml.org,2002:str', value=uni)
    return node

yaml.add_representer(unicode, unicode_representer)

This seems to make dumping unicode objects work the same as dumping str objects for me (Python 2.6).

In [72]: yaml.dump(u'abc')
Out[72]: 'abc\n...\n'

In [73]: yaml.dump('abc')
Out[73]: 'abc\n...\n'

In [75]: yaml.dump(['abc'])
Out[75]: '[abc]\n'

In [76]: yaml.dump([u'abc'])
Out[76]: '[abc]\n'
注定孤独终老 2024-08-23 08:44:32

您需要一个新的转储器类,它可以完成标准转储器类所做的所有事情,但覆盖 str 和 unicode 的表示。

from yaml.dumper import Dumper
from yaml.representer import SafeRepresenter

class KludgeDumper(Dumper):
   pass

KludgeDumper.add_representer(str,
       SafeRepresenter.represent_str)

KludgeDumper.add_representer(unicode,
        SafeRepresenter.represent_unicode)

这导致了

>>> print yaml.dump([u'abc',u'abc\xe7'],Dumper=KludgeDumper)
[abc, "abc\xE7"]

>>> print yaml.dump([u'abc',u'abc\xe7'],Dumper=KludgeDumper,encoding=None)
[abc, "abc\xE7"]

当然,我仍然不知道如何保持这个漂亮。

>>> print u'abc\xe7'
abcç

它破坏了后来的 yaml.load()

>>> yy=yaml.load(yaml.dump(['abc','abc\xe7'],Dumper=KludgeDumper,encoding=None))
>>> yy
['abc', 'abc\xe7']
>>> print yy[1]
abc�
>>> print u'abc\xe7'
abcç

You need a new dumper class that does everything the standard Dumper class does but overrides the representers for str and unicode.

from yaml.dumper import Dumper
from yaml.representer import SafeRepresenter

class KludgeDumper(Dumper):
   pass

KludgeDumper.add_representer(str,
       SafeRepresenter.represent_str)

KludgeDumper.add_representer(unicode,
        SafeRepresenter.represent_unicode)

Which leads to

>>> print yaml.dump([u'abc',u'abc\xe7'],Dumper=KludgeDumper)
[abc, "abc\xE7"]

>>> print yaml.dump([u'abc',u'abc\xe7'],Dumper=KludgeDumper,encoding=None)
[abc, "abc\xE7"]

Granted, I'm still stumped on how to keep this pretty.

>>> print u'abc\xe7'
abcç

And it breaks a later yaml.load()

>>> yy=yaml.load(yaml.dump(['abc','abc\xe7'],Dumper=KludgeDumper,encoding=None))
>>> yy
['abc', 'abc\xe7']
>>> print yy[1]
abc�
>>> print u'abc\xe7'
abcç
绅刃 2024-08-23 08:44:32

除了 interjay 的出色答案之外,如果您注意文件编码,则可以重新加载您的 unicode。

# -*- coding: utf-8 -*-
import yaml
import codecs

data = dict(key = u"abcç\U0001F511")

fn = "test2.yaml"
with codecs.open(fn, "w", encoding="utf-8") as fo:
    yaml.safe_dump(data, fo)

with codecs.open(fn, encoding="utf-8") as fi:
    data2 = yaml.safe_load(fi)

print ("data2:", data2, "type(data.key):", type(data2.get("key")) )

print data2.get("key")

test2.yaml 编辑器中的内容:

{key: "abc\xE7\uD83D\uDD11"}

打印输出:

('data2:', {'key ': u'abc\xe7\U0001f511'}, 'type(data.key):', )
abcç

little addition to interjay's excellent answer, you can keep your unicode on a reload if you take care of your file encodings.

# -*- coding: utf-8 -*-
import yaml
import codecs

data = dict(key = u"abcç\U0001F511")

fn = "test2.yaml"
with codecs.open(fn, "w", encoding="utf-8") as fo:
    yaml.safe_dump(data, fo)

with codecs.open(fn, encoding="utf-8") as fi:
    data2 = yaml.safe_load(fi)

print ("data2:", data2, "type(data.key):", type(data2.get("key")) )

print data2.get("key")

test2.yaml contents in my editor:

{key: "abc\xE7\uD83D\uDD11"}

print outputs:

('data2:', {'key': u'abc\xe7\U0001f511'}, 'type(data.key):', <type 'unicode'>)
abcç????

Plus, after reading http://nedbatchelder.com/blog/201302/war_is_peace.html I am pretty sure that safe_load/safe_dump is where I want to be anyway.

反话 2024-08-23 08:44:32

我刚刚开始使用 Python 和 YAML,但这可能也有帮助。只需比较输出:

def test_dump(self):
    print yaml.dump([{'name': 'value'}, {'name2': 1}], explicit_start=True)
    print yaml.dump_all([{'name': 'value'}, {'name2': 1}])

I've just started with Python and YAML, but probably this may also help. Just compare outputs:

def test_dump(self):
    print yaml.dump([{'name': 'value'}, {'name2': 1}], explicit_start=True)
    print yaml.dump_all([{'name': 'value'}, {'name2': 1}])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文