Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'output': 0.05; 'string': 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; 'subject:string': 0.09; 'python': 0.11; 'characters:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Formatting': 0.16; 'unicode)': 0.16; 'unicode.': 0.16; 'wrote:': 0.18; '(the': 0.22; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'unicode': 0.24; "i've": 0.25; 'right.': 0.26; 'switch': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'characters': 0.30; "i'm": 0.30; 'breaking': 0.31; 'guess': 0.33; 'subject:with': 0.35; "can't": 0.35; 'skip:u 20': 0.35; 'convert': 0.35; 'operations': 0.35; 'but': 0.35; 'there': 0.35; 'problems': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'first': 0.61; 'name': 0.63; 'soon': 0.63; 'containing': 0.69; 'anne': 0.84; 'avoids': 0.84; 'compare:': 0.84; 'subject:printing': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Formatting string with accented characters for printing Date: Sun, 18 Jan 2015 19:28:52 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Gmane-NNTP-Posting-Host: p57bdbc79.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421605741 news.xs4all.nl 2921 [2001:888:2000:d::a6]:42978 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83995 Jerry Rocteur wrote: > When I try and format output when there are accented characters the > output does not look right. > > e.g. > > 27 Angie Dickons 67,638 > 28 Anne MÉRESSE 64,825 > > So the strings containing accented characters print one less than > those that don't. > > I've tried both: > > print '{0:2} {1:25} {2} '.format( cnt, nam[num].encode('utf-8'), > steps[ind1]) > print "%3d %-25s %-7s" % ( cnt, nam[num].encode('utf-8'), steps[ind1]) > > I've searched but I can't see a solution.. > > I guess it is the way I'm printing nam[num].encode('utf-8') perhaps I > have to convert it first ? If you have a byte string (the standard in Python 2) you have to decode(), i. e. convert it to unicode) before you format it. Compare: >>> names = "Angie Dickons", "Anne Méresse" >>> for name in names: ... print "|{:20}|".format(name) ... |Angie Dickons | |Anne Méresse | >>> for name in names: ... name = name.decode("utf-8") ... print u"|{:20}|".format(name) ... |Angie Dickons | |Anne Méresse | The best approach is to convert your data to unicode as soon as you read it and perform all string operations with unicode. This also avoids breaking characters: >>> print "Méresse"[:2] M� >>> print u"Méresse"[:2] Mé There are still problems (e. g. with narrow builds), and the best way to avoid a few string-related inconviences is to switch to Python 3.