Formated String in optparse

Peter Otten __peter__ at web.de
Fri Apr 15 01:48:43 EDT 2005


Norbert Thek wrote:

> Thank You for your help, its working!
> 
> Now I have an additional question.

...which would warrant a separate thread...

> The problem is the encoding of the Text
> I'm using German, Can you tell me how to encode
> the textstring that the Windows commandline shows the special letters
> right?
> For exampel i get 'f³r' but i want 'für'  (maybe reader with only an
> english enabled browser wouldn't see a difference..)
> 
> I tried to work with the encode method of string but It didn't work for me
> some hint what to do?

>>> u"f³r" # what you have (minus unicode)
u'f\xb3r'

>>> u"für" 
u'f\xfcr' # what you need

One way to get that (wrong) representation: 

>>> u"für".encode("cp1252").decode("cp850")
u'f\xb3r'

So it could be that you are interpreting cp1252 ("German" windows) as cp850
("German" DOS).

Try the following script:

# -*- coding: cp1252 -*-
text = u"text with umlauts äöü ÄÖÜ ß".encode("cp850")
print text


If that works correctly, you can prepare every string literal in your
program in the same way. Or redirect sys.stdout

sys.stdout = codecs.getwriter("cp850")(sys.stdout)

as posted by Martin von Löwis on de.comp.lang.python only two days ago.

Peter









More information about the Python-list mailing list