pyyaml: dumping without tags

前端 未结 5 1969
囚心锁ツ
囚心锁ツ 2021-01-30 19:45

I have

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

But I want

>>> import         


        
相关标签:
5条回答
  • 2021-01-30 20:04

    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.

    0 讨论(0)
  • 2021-01-30 20:04

    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}])
    
    0 讨论(0)
  • 2021-01-30 20:21

    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'
    
    0 讨论(0)
  • 2021-01-30 20:22

    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ç

    0 讨论(0)
  • 2021-01-30 20:25

    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ç
    
    0 讨论(0)
提交回复
热议问题