Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #109288 > unrolled thread

How to print unicode characters with yaml.safe_dump()?

Started byPeng Yu <pengyu.ut@gmail.com>
First post2016-05-31 11:58 -0500
Last post2016-05-31 11:58 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  How to print unicode characters with yaml.safe_dump()? Peng Yu <pengyu.ut@gmail.com> - 2016-05-31 11:58 -0500

#109288 — How to print unicode characters with yaml.safe_dump()?

FromPeng Yu <pengyu.ut@gmail.com>
Date2016-05-31 11:58 -0500
SubjectHow to print unicode characters with yaml.safe_dump()?
Message-ID<mailman.56.1464713892.1839.python-list@python.org>
Hi, The following code shows that "Michał" is printed differently for
print(yaml.safe_dump(...)) and the direct print. Does anybody know how
to use yaml.safe_dump() so that "Michał" will be printed as is.

~$ cat main.py
#!/usr/bin/env python
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:

import yaml

foo = {
    u'first': u"Michał",
    u'last': u"Seweryn",
}

print foo['first']

print(yaml.safe_dump(foo, default_flow_style=True).encode('utf-8'))
print(yaml.safe_dump(foo, default_flow_style=False).encode('utf-8'))
~$ ./main.py
Michał
{first: "Micha\u0142", last: Seweryn}

first: "Micha\u0142"
last: Seweryn

-- 
Regards,
Peng

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web