Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed0.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: How to print unicode characters with yaml.safe_dump()? Date: Tue, 31 May 2016 19:38:31 +0200 Organization: None Lines: 37 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: news.uni-berlin.de nFhsyxZ0Eo0Fe+1uimexNQiMZ3dqk/YRdokssQftFllg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'differently': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'python': 0.10; 'first:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:unicode': 0.16; 'yaml': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'skip:# 10': 0.27; 'cat': 0.29; 'print': 0.30; 'code': 0.30; 'anybody': 0.32; 'skip:d 40': 0.32; 'foo': 0.33; 'skip:d 20': 0.34; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'hi,': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'received:de': 0.40; 'is.': 0.63; 'direct': 0.68 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd8584.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:109289 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