Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

right adjusted strings containing umlauts

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


kurt.alfred.mueller at gmail

Aug 8, 2013, 7:23 AM

Post #1 of 15 (52 views)
Permalink
right adjusted strings containing umlauts

I'd like to print strings right adjusted.
( Python 2.7.3, Linux 3.4.47-2.38-desktop )

from __future__ import print_function
print( '>{0:>3}<'.format( 'a' ) )
> a<

But if the string contains an Umlaut:
print( '>{0:>3}<'.format( 'ä' ) )
> ä<

Same with % notation:
print( '>%3s<' % ( 'a' ) )
> a<
print( '>%3s<' % ( 'ä' ) )
> ä<

For a string with no Umlaut it uses 3 characters, but for an Umlaut
it uses only 2 characters.

I guess it has to to with unicode.
How do I get it right?


TIA
--
Kurt Mueller

--
http://mail.python.org/mailman/listinfo/python-list


neilc at norwich

Aug 8, 2013, 7:40 AM

Post #2 of 15 (51 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

On 2013-08-08, Kurt Mueller <kurt.alfred.mueller [at] gmail> wrote:
> I'd like to print strings right adjusted.
> ( Python 2.7.3, Linux 3.4.47-2.38-desktop )
>
> from __future__ import print_function
> print( '>{0:>3}<'.format( 'a' ) )
>> a<
>
> But if the string contains an Umlaut:
> print( '>{0:>3}<'.format( '??' ) )
>> ??<
>
> Same with % notation:
> print( '>%3s<' % ( 'a' ) )
>> a<
> print( '>%3s<' % ( '??' ) )
>> ??<
>
> For a string with no Umlaut it uses 3 characters, but for an
> Umlaut it uses only 2 characters.
>
> I guess it has to to with unicode.
> How do I get it right?

You guessed it!

Use unicode strings instead of byte strings, e.g., u"...".

--
Neil Cerutti
--
http://mail.python.org/mailman/listinfo/python-list


jfharden at gmail

Aug 8, 2013, 7:43 AM

Post #3 of 15 (51 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

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)

--
Jonathan
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Aug 8, 2013, 8:19 AM

Post #4 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

On 08/08/2013 15:40, Neil Cerutti wrote:
> On 2013-08-08, Kurt Mueller <kurt.alfred.mueller [at] gmail> wrote:
>> I'd like to print strings right adjusted.
>> ( Python 2.7.3, Linux 3.4.47-2.38-desktop )
>>
>> from __future__ import print_function
>> print( '>{0:>3}<'.format( 'a' ) )
>>> a<
>>
>> But if the string contains an Umlaut:
>> print( '>{0:>3}<'.format( '??' ) )
>>> ??<
>>
>> Same with % notation:
>> print( '>%3s<' % ( 'a' ) )
>>> a<
>> print( '>%3s<' % ( '??' ) )
>>> ??<
>>
>> For a string with no Umlaut it uses 3 characters, but for an
>> Umlaut it uses only 2 characters.
>>
>> I guess it has to to with unicode.
>> How do I get it right?
>
> You guessed it!
>
> Use unicode strings instead of byte strings, e.g., u"...".
>
It also matters which actual codepoints you're using in the Unicode
string.

You could have u'ä', which is one codepoint (u'\xE4' or u'\N{LATIN
SMALL LETTER A WITH DIAERESIS}'), or u'ä', which two codepoints
(u'a\u0308' or u'\N{LATIN SMALL LETTER A}\N{COMBINING DIAERESIS}').

--
http://mail.python.org/mailman/listinfo/python-list


kurt.alfred.mueller at gmail

Aug 8, 2013, 8:24 AM

Post #5 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Am 08.08.2013 16:43, schrieb jfharden [at] gmail:
> 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?

TIA
--
Kurt Mueller

--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Aug 8, 2013, 8:44 AM

Post #6 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Kurt Mueller wrote:

> Am 08.08.2013 16:43, schrieb jfharden [at] gmail:
>> 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


--
http://mail.python.org/mailman/listinfo/python-list


davea at davea

Aug 8, 2013, 8:50 AM

Post #7 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Kurt Mueller wrote:

> Am 08.08.2013 16:43, schrieb jfharden [at] gmail:
>> 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?
>

In general, when reading from an outside device like a file, convert to
unicode immediately, while you still know the encoding used in that
particular file. Then after all processing, worry about alignment only
when you're about to output the string. And at that point, you're
subject to the quirks of the font as well as the quirks of the
encoding of the terminal.

As MRAB has pointed out, sometimes two code points are used to represent
a single character which will end up taking a single column. Likewise
sometimes a single code point will take more than one "column" to
display. Ideograms are one example, but a font which is not fixed pitch
is another.

If you're going to a standard terminal, all you can do is get close.
This is why there are special functions for gui's to help with
alignment.



--
DaveA


--
http://mail.python.org/mailman/listinfo/python-list


kurt.alfred.mueller at gmail

Aug 8, 2013, 9:16 AM

Post #8 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Am 08.08.2013 17:44, schrieb Peter Otten:
> Kurt Mueller wrote:
>> 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']

How do I get to know the actual encoding?
I read from stdin. There can be different encondings.
Usually utf8 but also iso-8859-1/latin9 are to be expected.
But sys.stdin.encoding sais always 'None'.


TIA
--
Kurt Mueller
--
http://mail.python.org/mailman/listinfo/python-list


kurt.alfred.mueller at gmail

Aug 8, 2013, 9:27 AM

Post #9 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Now I have this small example:
----------------------------------------------------------
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

from __future__ import print_function
import sys, shlex

print( repr( sys.stdin.encoding ) )

strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'
for inpt_line in sys.stdin:
proc_line = shlex.split( inpt_line, False, True, )
encoding = "utf-8"
proc_line = [ strg.decode( encoding ) for strg in proc_line ]
print( strg_form.format( *proc_line ) )
----------------------------------------------------------

$ echo -e "a b c d e\na ö u 1 2" | file -
/dev/stdin: UTF-8 Unicode text
$ echo -e "a b c d e\na ö u 1 2" | ./align_compact.py
None
a b c d e
a ö u 1 2
$ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | file -
/dev/stdin: ISO-8859 text
$ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | ./align_compact.py
None
a b c d e
Traceback (most recent call last):
File "./align_compact.py", line 13, in <module>
proc_line = [ strg.decode( encoding ) for strg in proc_line ]
File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte
muk [at] mcp2:/sw/prog/scripts/text_manip>

How do I handle this two inputs?


TIA
--
Kurt Mueller

--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Aug 8, 2013, 9:34 AM

Post #10 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Kurt Mueller wrote:

> Am 08.08.2013 17:44, schrieb Peter Otten:
>> Kurt Mueller wrote:
>>> 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']
>
> How do I get to know the actual encoding?
> I read from stdin. There can be different encondings.
> Usually utf8 but also iso-8859-1/latin9 are to be expected.
> But sys.stdin.encoding sais always 'None'.

Even with

$ cat funny_pic.jpg | ./mypythonscript.py

you could "successfully" (i. e. no errors) decode stdin using iso-8859-1.
So unfortunately you have to guess.

A simple strategy is to try utf-8 and fall back to iso-8859-1 if that fails
with a UnicodeDecodeError. There's also

https://pypi.python.org/pypi/chardet


--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Aug 8, 2013, 9:37 AM

Post #11 of 15 (47 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

On Thu, Aug 8, 2013 at 5:16 PM, Kurt Mueller
<kurt.alfred.mueller [at] gmail> wrote:
> Am 08.08.2013 17:44, schrieb Peter Otten:
>> Kurt Mueller wrote:
>>> 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']
>
> How do I get to know the actual encoding?
> I read from stdin. There can be different encondings.
> Usually utf8 but also iso-8859-1/latin9 are to be expected.
> But sys.stdin.encoding sais always 'None'.

If you can switch to Python 3, life becomes a LOT easier. The Python 3
input() function (which does the same job as raw_input() from Python
2) returns a Unicode string, meaning that it takes care of encodings
for you.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


davea at davea

Aug 8, 2013, 10:47 AM

Post #12 of 15 (38 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Kurt Mueller wrote:

> Now I have this small example:
> ----------------------------------------------------------
> #!/usr/bin/env python
> # vim: set fileencoding=utf-8 :
>
> from __future__ import print_function
> import sys, shlex
>
> print( repr( sys.stdin.encoding ) )
>
> strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'
> for inpt_line in sys.stdin:
> proc_line = shlex.split( inpt_line, False, True, )
> encoding = "utf-8"
> proc_line = [ strg.decode( encoding ) for strg in proc_line ]
> print( strg_form.format( *proc_line ) )
> ----------------------------------------------------------
>
> $ echo -e "a b c d e\na ö u 1 2" | file -
> /dev/stdin: UTF-8 Unicode text
> $ echo -e "a b c d e\na ö u 1 2" | ./align_compact.py
> None
> a b c d e
> a ö u 1 2
> $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | file -
> /dev/stdin: ISO-8859 text
> $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | ./align_compact.py
> None
> a b c d e
> Traceback (most recent call last):
> File "./align_compact.py", line 13, in <module>
> proc_line = [ strg.decode( encoding ) for strg in proc_line ]
> File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
> return codecs.utf_8_decode(input, errors, True)
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte
> muk [at] mcp2:/sw/prog/scripts/text_manip>
>
> How do I handle this two inputs?
>

Once you're using pipes, you've given up any hope that the terminal will
report a useful encoding, so I'm not surprised you're getting None for
sys.stdin.encoding()

So you can either do as others have suggested, and guess, or you can get
the information explicitly, say from argv. In any case you'll need a
different way to assign encoding =


--
DaveA

--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Aug 8, 2013, 1:51 PM

Post #13 of 15 (36 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

On 8/8/2013 11:24 AM, Kurt Mueller wrote:

> print( u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'.format( *output_list ) )

Using autonumbering feature, same as

print( u'{:>3} {:>3} {:>3} {:>3} {:>3}'.format( *output_list ) )
print( (u' '.join([u'{:>3}']*5)).format(*output_list) )
print( (u' '.join([u'{:>3}']*len(output_list))).format(*output_list) )
--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


wxjmfauth at gmail

Aug 9, 2013, 1:30 AM

Post #14 of 15 (32 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

Le jeudi 8 août 2013 18:27:06 UTC+2, Kurt Mueller a écrit :
> Now I have this small example:
>
> ----------------------------------------------------------
>
> #!/usr/bin/env python
>
> # vim: set fileencoding=utf-8 :
>
>
>
> from __future__ import print_function
>
> import sys, shlex
>
>
>
> print( repr( sys.stdin.encoding ) )
>
>
>
> strg_form = u'{0:>3} {1:>3} {2:>3} {3:>3} {4:>3}'
>
> for inpt_line in sys.stdin:
>
> proc_line = shlex.split( inpt_line, False, True, )
>
> encoding = "utf-8"
>
> proc_line = [ strg.decode( encoding ) for strg in proc_line ]
>
> print( strg_form.format( *proc_line ) )
>
> ----------------------------------------------------------
>
>
>
> $ echo -e "a b c d e\na ö u 1 2" | file -
>
> /dev/stdin: UTF-8 Unicode text
>
> $ echo -e "a b c d e\na ö u 1 2" | ./align_compact.py
>
> None
>
> a b c d e
>
> a ö u 1 2
>
> $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | file -
>
> /dev/stdin: ISO-8859 text
>
> $ echo -e "a b c d e\na ö u 1 2" | recode utf8..latin9 | ./align_compact.py
>
> None
>
> a b c d e
>
> Traceback (most recent call last):
>
> File "./align_compact.py", line 13, in <module>
>
> proc_line = [ strg.decode( encoding ) for strg in proc_line ]
>
> File "/usr/lib64/python2.7/encodings/utf_8.py", line 16, in decode
>
> return codecs.utf_8_decode(input, errors, True)
>
> UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 0: invalid start byte
>
> muk [at] mcp2:/sw/prog/scripts/text_manip>
>
>
>
> How do I handle this two inputs?
>
>
>
>
>
> TIA
>
> --
>
> Kurt Mueller

--------

It's very easy.

The error msg indicates, you cann't decode your series of bytes
with the utf-8 codec, simply because your string is encoded
in iso-8859-* (you did it explicitly!).


Your problem is not Python, your problem is the coding
of the characters.

You should be aware about the coding of the strings you are
manipulating (creating) and if necessary decode and/or encode
correctly accordingly to what you wish, eg. a suitable coding
for the display. That's on this level that Python (or any
language) matters.

The sys.std*.encoding is a different problem.

iso-8859-* ?

iso-8859-1 == latin-1 and latin9 == iso-8859-15.

If one excepts "das grosse Eszett", both codings are
able to handle German (it seems to be your case) and
there are no problems when working directly with these
codings.


jmf



--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Aug 9, 2013, 6:29 PM

Post #15 of 15 (29 views)
Permalink
Re: right adjusted strings containing umlauts [In reply to]

On Thu, 08 Aug 2013 17:24:49 +0200, Kurt Mueller wrote:

> What do I do, when input_strings/output_list has other codings like
> iso-8859-1?

When reading from a text file, honour some sort of encoding cookie at the
top (or bottom) of the file, like Emacs and Vim use, or a BOM. If there
is no encoding cookie, assume UTF-8.

When reading from stdin, assume UTF-8.

Otherwise, make it the caller's responsibility to specify the encoding if
they wish to use something else.

Pseudo-code:

encoding = None

if command line arguments include '--encoding':
encoding = --encoding argument

if encoding is None:
if input file is stdin:
encoding = 'utf-8'
else:
open file as binary
if first 2-4 bytes look like a BOM:
encoding = one of UTF-8 or UTF-16 or UTF-32
else:
read first two lines
if either looks like an encoding cookie:
encoding = cookie
# optionally check the end of the file as well
close file

if encoding is None:
encoding = 'utf-8'

read from file using encoding




--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.