Alphanumerics

Fredrik Lundh fredrik at pythonware.com
Wed Nov 10 05:03:44 EST 1999


Alexis <alexisjuh at usa.net> wrote:
> I'm making some software development for a python program created in the US,
> well, it's pretty much just translating and adding some language specific
> modules for Norway, Sweden and Denmark...Problem is the special chars we have
> here, like åæø for norwegian..
> When I use a string everything works fine, but when I import fields from
> databases and they contain these chars I'll get escape seq instead...
> The chr(num) function doesn't work for these chars for some reason, and now
> I'm wondering if there's another way to get it right...

this is not really a problem in python, but the
environment can make it look much harder than
it is.   here are some basic facts, to help you
figure out what's going wrong.

    1. python's string type has no problem with
       non-ascii characters (as long as we're talking
       8-bit character sets, that is).  chr() and ord()
       works as you'd expect -- they convert between
       8-bit characters and unsigned integers.

    2. python has no default encoding -- you have
       to keep track of that yourself (or, easier, make
       sure you convert anything to, say, Latin 1).  if
       you don't know what encoding your data are
       using, figure that out before attempting to do
       something with that data.

    3. in cases where python uses "repr" to prepare
       strings for printing, it converts non-ascii character
       to /ddd representation, instead of sending them
       to the output device as is.

    4. the python interpreter uses "repr" to format
       strings when run in interactive mode, unless
       you use print:

            >>> s = "åäö"
            >>> s
            '\345\344\366'
            >>> print s
            åäö

    5. while Windows usually use ISO Latin 1, MS-DOS
       (aka CONSOLE) WINDOWS DON'T.

            >>> s = "åäö"
            >>> s
            '\206\204\224'
            >>> print s
            åäö

        (note the different output from repr).

okay, that's enough for now.  let us know if you still cannot
figure out where things go wrong in your program.

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list