Is there a way to change the default string encoding?

Discussion in 'Python' started by Ron Garret, Aug 21, 2007.

  1. Ron Garret

    Ron Garret Guest

    Is there a way to change the default string encoding used by the
    string.encode() method? My default environment is utf-8 but I need it
    to be latin-1 to avoid errors like this:

    >>> 'Andr\xe9 Ramel'.decode()

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 4:
    ordinal not in range(128)

    I can't change the code to pass an encoding argument to the decode
    method because it's someone else's code.

    Thanks,
    rg
    Ron Garret, Aug 21, 2007
    #1
    1. Advertising

  2. Ron Garret

    Peter Otten Guest

    Ron Garret wrote:

    > Is there a way to change the default string encoding used by the
    > string.encode() method?


    encode() or decode()? Encoding is best handled by the output stream, e. g.
    passing codecs.open(...) instead of the builtin open(...).


    > My default environment is utf-8 but I need it
    > to be latin-1 to avoid errors like this:
    >
    >>>> 'Andr\xe9 Ramel'.decode()

    > Traceback (most recent call last):
    > File "<stdin>", line 1, in <module>
    > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 4:
    > ordinal not in range(128)


    If your environment were latin-1, you'd get the same error because Python
    assumes ascii by default.

    > I can't change the code to pass an encoding argument to the decode
    > method because it's someone else's code.


    Does that code accept unicode strings? Try to pass u"Andre\xe9 Ramel"
    instead of the byte string.

    If all else fails there's

    >>> sys.setdefaultencoding("latin1")
    >>> "Andre\xe9 Ramel".decode()

    u'Andre\xe9 Ramel'

    but that's an evil hack, you should rather talk to the maintainer of the
    offending code to update it to accept unicode.

    Peter
    Peter Otten, Aug 21, 2007
    #2
    1. Advertising

  3. Ron Garret

    Ron Garret Guest

    In article <fadvvr$ogf$00$-online.com>,
    Peter Otten <> wrote:

    > If all else fails there's
    >
    > >>> sys.setdefaultencoding("latin1")
    > >>> "Andre\xe9 Ramel".decode()

    > u'Andre\xe9 Ramel'
    >
    > but that's an evil hack, you should rather talk to the maintainer of the
    > offending code to update it to accept unicode.


    Yes, but I need to hack around it until I can get it fixed.

    Thanks!

    rg
    Ron Garret, Aug 21, 2007
    #3
  4. Il Mon, 20 Aug 2007 18:44:39 -0700, Ron Garret ha scritto:

    > Is there a way to change the default string encoding ...


    Dive Into Python. Section 9 on http://diveintopython.org/xml_processing/
    unicode.html

    That will help.

    Bye
    Fabio
    Fabio Z Tessitore, Aug 21, 2007
    #4
  5. Ron Garret

    Peter Otten Guest

    Ron Garret wrote:

    > In article <fadvvr$ogf$00$-online.com>,
    > Peter Otten <> wrote:
    >
    >> If all else fails there's
    >>
    >> >>> sys.setdefaultencoding("latin1")
    >> >>> "Andre\xe9 Ramel".decode()

    >> u'Andre\xe9 Ramel'
    >>
    >> but that's an evil hack, you should rather talk to the maintainer of the
    >> offending code to update it to accept unicode.

    >
    > Yes, but I need to hack around it until I can get it fixed.


    Oops, the snippet above omits the actual hack. It should be

    >>> import sys
    >>> reload(sys)

    <module 'sys' (built-in)>
    >>> sys.setdefaultencoding("latin1")
    >>> "Andre\xe9 Ramel".decode()

    u'Andre\xe9 Ramel'

    Peter
    Peter Otten, Aug 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ThunderMusic
    Replies:
    1
    Views:
    4,434
    Chris Taylor
    Feb 12, 2006
  2. Hardy Wang

    Encoding.Default and Encoding.UTF8

    Hardy Wang, Jun 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    18,832
    Jon Skeet [C# MVP]
    Jun 9, 2004
  3. Ace_12
    Replies:
    0
    Views:
    906
    Ace_12
    Sep 22, 2006
  4. Replies:
    2
    Views:
    1,244
    Marcelo Morales
    Apr 3, 2008
  5. cs_professional
    Replies:
    14
    Views:
    5,228
    cs_professional
    Dec 12, 2010
Loading...

Share This Page