right adjusted strings containing umlauts

Peter Otten __peter__ at web.de
Thu Aug 8 11:44:26 EDT 2013


Kurt Mueller wrote:

> Am 08.08.2013 16:43, schrieb jfharden at gmail.com:
>> On Thursday, 8 August 2013 15:23:46 UTC+1, Kurt Mueller  wrote:
>>> I'd like to print strings right adjusted.
>>> print( '>{0:>3}<'.format( 'ä' ) )
>> 
>> Make both strings unicode
>> print( u'>{0:>3}<'.format( u'ä' ) )
>> Why not use rjust for it though?
>> u'ä'.rjust(3)
> 
> In real life there is a list of strings in output_list from a command
> like: output_list = shlex.split( input_string, bool_cmnt, bool_posi, )
> input_string is from a file, bool_* are either True or False
> repr( output_list )
> ['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f']
> which should be printed right aligned.
> using:
> print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) )
> ( In real life, the alignement and the width is variable )
> 
> How do I prepare output_list the pythonic way to be unicode strings?
> What do I do, when input_strings/output_list has other codings like
> iso-8859-1?

You have to know the actual encoding. With that information it's easy:

>>> output_list
['\xc3\xb6', '\xc3\xbc', 'i', 's', 'f']
>>> encoding = "utf-8"
>>> output_list = [s.decode(encoding) for s in output_list]
>>> print output_list
[u'\xf6', u'\xfc', u'i', u's', u'f']

Don't worry that there are still escape codes -- when you print the 
individual list items the caracters will show up as expected:

>>> print ", ".join(output_list)
ö, ü, i, s, f





More information about the Python-list mailing list