Unicode again ... default codec ...

Stef Mientki stef.mientki at gmail.com
Tue Oct 20 16:13:52 EDT 2009


hello,

As someone else already said,
"every time I think : now I understand it completely, and a few weeks 
later ..."

Form the thread "how to write a unicode string to a file ?"
and my specific situation:

- reading data from Excel, Delphi and other Windows programs and unicode 
Python
- using wxPython, which forces unicode
- writing to Excel and other Windows programs

almost all answers, directed to the following solution:
- in the python program, turn every string as soon as possible into unicode
- in Python all processing is done in unicode
- at the end, translate unicode into the windows specific character set 
(if necessary)

The above approach seems to work nicely,
but manipulating heavily with string like objects it's a crime.
It's impossible to change all my modules from strings to unicode at once,
and it's very tempting to do it just the opposite : convert everything 
into strings !

# adding unicode string and windows strings, results in an error:
my_u = u'my_u'
my_w = 'my_w' + chr ( 246 )
x = my_s + my_u

# to correctly handle the above ( in my situation), I need to write the 
following code (which my code quite unreadable
my_u = u'my_u'
my_w = 'my_w' + chr ( 246 )
x = unicode ( my_s, 'windows-1252' )  + my_u

# converting to strings gives much better readable code:
my_u = u'my_u'
my_w = 'my_w' + chr ( 246 )
x = my_s + str(my_u)

until I found this website:
  http://diveintopython.org/xml_processing/unicode.html

By settings the default encoding:
I now can go to unicode much more elegant and almost fully automatically:
(and I guess the writing to a file problem is also solved)
# now the manipulations of strings and unicode works OK:
my_u = u'my_u'
my_w = 'my_w' + chr ( 246 )
x = my_s + my_u

The only disadvantage is that you've to put a special named file into 
the Python directory !!
So if someone knows a more elegant way to set the default codec,
I would be much obliged.

cheers,
Stef








More information about the Python-list mailing list