Unicode question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben Edwards (lists)

    #1

    Unicode question

    I am using python 2.4 on Ubuntu dapper, I am working through Dive into
    Python.

    There are a couple of inconsictencies .

    Firstly sys.setdefaulte ncoding('iso− 8859−1') does not work,I have to do
    sys.setdefaulte ncoding = 'iso−8859−1 '

    secondly the following does not give a 'UnicodeError: ASCII encoding
    error:', and I would expect ti to. In fact it prints out the n with ~
    above it fine:

    sys.setdefaulte ncoding = 'ascii'
    s = u'La Pe\xf1a'
    print s

    Any insight?
    Ben


  • Steve M

    #2
    Re: Unicode question

    Ben Edwards (lists) wrote:
    I am using python 2.4 on Ubuntu dapper, I am working through Dive into
    Python.
    >
    There are a couple of inconsictencies .
    >
    Firstly sys.setdefaulte ncoding('iso-8859-1') does not work, I have to do
    sys.setdefaulte ncoding = 'iso-8859-1'
    When you run a Python script, the interpreter does some of its own
    stuff before executing your script. One of the things it does is to
    delete the name sys.setdefaulte ncoding. This means that by the time
    even your first line of code runs that name no longer exists and so you
    will be unable to invoke the function as in your first attempt.

    The second attempt "sys.setdefault encoding = 'iso-8859-1' " is creating
    a new name under the sys namespace and assigning it a string. This will
    not have the desired effect, or probably any effect at all.

    I have found that in order to change the default encoding with that
    function, you can put the command in a file called sitecustomize.p y
    which, when placed in the appropriate location (which is
    platform-dependent), will be called in time to have the desired effect.

    So the order of events is something like:
    1. Invoke Python on myscript.py
    2. Python does some stuff and then executes sitecustomize.p y
    3. Python deletes the name sys.setdefaulte ncoding, thereby making the
    function that was so-named inaccessible.
    4. Python then begins executing myscript.py.


    Regarding the location of sitecustomize.p y, on Windows it is
    C:\Python24\Lib \sitecustomize. py.

    My guess is that you should put it in the same directory as the bulk of
    the Python standard library files. (Also in that directory is a
    subdirectory called site-packages, where you can put custom modules
    that will be available for import from any of your scripts.)

    Comment

    • Martin v. Löwis

      #3
      Re: Unicode question

      Ben Edwards (lists) wrote:
      Firstly sys.setdefaulte ncoding('iso− 8859−1') does not work, I have to do
      sys.setdefaulte ncoding = 'iso−8859−1 '
      That "works", but has no effect. You bind the variable
      sys.setdefaulte ncoding to some value, but that value is never used for
      anything (do sys.getdefaulte ncoding() to see what I mean). You could
      just as well write

      sys.standardkod ierung = 'iso-8859-1'
      secondly the following does not give a 'UnicodeError: ASCII encoding
      error:', and I would expect ti to. In fact it prints out the n with ~
      above it fine:
      >
      sys.setdefaulte ncoding = 'ascii'
      s = u'La Pe\xf1a'
      print s
      >
      Any insight?
      The print statement uses sys.stdout.enco ding, not the default encoding.

      Regards,
      Martin

      Comment

      Working...