Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109289
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: How to print unicode characters with yaml.safe_dump()? |
| Date | 2016-05-31 19:38 +0200 |
| Organization | None |
| Message-ID | <mailman.57.1464716322.1839.python-list@python.org> (permalink) |
| References | <CABrM6wmj3Hi0ZxPCvsgBnSXuNk367jBbU70QDuOpJ7ZrUcbsKA@mail.gmail.com> <niki6o$2dk$1@ger.gmane.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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: How to print unicode characters with yaml.safe_dump()? Peter Otten <__peter__@web.de> - 2016-05-31 19:38 +0200
csiph-web