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


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

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

Started byPeter Otten <__peter__@web.de>
First post2016-05-31 19:38 +0200
Last post2016-05-31 19:38 +0200
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

  Re: How to print unicode characters with yaml.safe_dump()? Peter Otten <__peter__@web.de> - 2016-05-31 19:38 +0200

#109289 — Re: How to print unicode characters with yaml.safe_dump()?

FromPeter Otten <__peter__@web.de>
Date2016-05-31 19:38 +0200
SubjectRe: How to print unicode characters with yaml.safe_dump()?
Message-ID<mailman.57.1464716322.1839.python-list@python.org>
Peng Yu wrote:

> 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
> 

Use the allow_unicode flag:

>>> print yaml.safe_dump(foo, allow_unicode=True, default_flow_style=False)
first: Michał
last: Seweryn

[toc] | [standalone]


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


csiph-web