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


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

Re: Formatting string with accented characters for printing

Started byPeter Otten <__peter__@web.de>
First post2015-01-18 19:28 +0100
Last post2015-01-26 06:31 -0800
Articles 2 — 2 participants

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: Formatting string with accented characters for printing Peter Otten <__peter__@web.de> - 2015-01-18 19:28 +0100
    Re: Formatting string with accented characters for printing wxjmfauth@gmail.com - 2015-01-26 06:31 -0800

#83995 — Re: Formatting string with accented characters for printing

FromPeter Otten <__peter__@web.de>
Date2015-01-18 19:28 +0100
SubjectRe: Formatting string with accented characters for printing
Message-ID<mailman.17841.1421605741.18130.python-list@python.org>
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.

[toc] | [next] | [standalone]


#84602

Fromwxjmfauth@gmail.com
Date2015-01-26 06:31 -0800
Message-ID<a3cc60c7-ca6b-4c9a-a4a4-887b9cf34ecf@googlegroups.com>
In reply to#83995
Le dimanche 18 janvier 2015 19:29:12 UTC+1, Peter Otten a écrit :
> 
> 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.

Well,

>>> print 'Méresse'
Méresse
>>> print u'Méresse'
Méresse
>>> print 'Méresse'[:2]
Mé
>>> print u'Méresse'[:2]
Mé
>>> sys.version
'2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]'
>>>

jmf

[toc] | [prev] | [standalone]


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


csiph-web