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?
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 need a new dumper class that does everything the standard Dumper class does but overrides the representers for str and unicode.
Which leads to
Granted, I'm still stumped on how to keep this pretty.
And it breaks a later yaml.load()
I've just started with Python and YAML, but probably this may also help. Just compare outputs:
You can use
safe_dump
instead ofdump
. Just keep in mind that it won't be able to represent arbitrary Python objects then. Also, when youload
the YAML, you will get astr
object instead ofunicode
.little addition to interjay's excellent answer, you can keep your unicode on a reload if you take care of your file encodings.
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ç
How about this:
This seems to make dumping unicode objects work the same as dumping str objects for me (Python 2.6).