string processing question

Kurt Mueller mu at problemlos.ch
Thu Apr 30 18:55:04 CEST 2009


Hi,


on a Linux system and python 2.5.1 I have the
following behaviour which I do not understand:



case 1
> python -c 'a="ä"; print a ; print a.center(6,"-") ; b=unicode(a, "utf8"); print b.center(6,"-")'
ä
--ä--
--ä---
>


case 2
----- an UnicodeEncodeError in this case:
> python -c 'a="ä"; print a ; print a.center(20,"-") ; b=unicode(a, "utf8"); print b.center(20,"-")' | cat
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 9: ordinal not in range(128)
ä
--ä--
>


The behaviour changes if I pipe the output to another prog or to a file.
and
centering with the string a is not correct, but with string b.



Could somebody please explain this to me?




Thanks in advance
-- 
Kurt Müller, mu at problemlos.ch




More information about the Python-list mailing list