How to do special encode in string ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fowlertrainer@anonym.hu

    How to do special encode in string ?

    Hi !

    I'm hungarian, we use special characters like:
    á - a'
    õ -o"

    etc.

    I want to encode this characters to in config file I see these
    characters as \nnn format.
    And I want to decode it automatically with python.

    How to I do it without write complex converter tool ?

    Thanx for it:
    FT

    Example:
    Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"

    Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"

  • Duncan Booth

    #2
    Re: How to do special encode in string ?

    "fowlertrainer@ anonym.hu" <fowlertrainer@ anonym.hu> wrote in
    news:mailman.88 .1087811553.454 .python-list@python.org :
    [color=blue]
    > Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"
    >
    > Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"
    >[/color]
    [color=blue][color=green][color=darkred]
    >>> s = "az \xe1llam \xe9n vagyok"
    >>> print s.decode('latin-1')[/color][/color][/color]
    az állam én vagyok[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    You want to use unicode strings if you have characters outside the ASCII
    range. The decode method on a byte string will let you convert it to a
    unicode string, and the encode method will let you convert it back to byte
    string.

    The tricky bit is that you need to know the correct encoding to use as \xe1
    could mean different characters, but in this case it looks as though you
    meant latin-1.

    Comment

    • Christopher Koppler

      #3
      Re: How to do special encode in string ?

      On 21 Jun 2004 10:14:57 GMT, Duncan Booth <me@privacy.net > wrote:
      [color=blue]
      >"fowlertrainer @anonym.hu" <fowlertrainer@ anonym.hu> wrote in
      >news:mailman.8 8.1087811553.45 4.python-list@python.org :
      >[color=green]
      >> Encode("az állam én vagyok") -> "az \xe1llam \xe9n vagyok"
      >>
      >> Decode("az \xe1llam \xe9n vagyok") -> "az állam én vagyok"
      >>[/color]
      >[color=green][color=darkred]
      >>>> s = "az \xe1llam \xe9n vagyok"
      >>>> print s.decode('latin-1')[/color][/color]
      >az állam én vagyok[color=green][color=darkred]
      >>>>[/color][/color]
      >
      >You want to use unicode strings if you have characters outside the ASCII
      >range. The decode method on a byte string will let you convert it to a
      >unicode string, and the encode method will let you convert it back to byte
      >string.
      >
      >The tricky bit is that you need to know the correct encoding to use as \xe1
      >could mean different characters, but in this case it looks as though you
      >meant latin-1.[/color]

      For Hungarian long umlauts, you'll want to use latin-2 (or iso8859-2).


      --
      Christopher

      Comment

      • Chris King

        #4
        Re: How to do special encode in string ?

        > Encode("az llam n vagyok") -> "az \xe1llam \xe9n vagyok"[color=blue]
        >
        > Decode("az \xe1llam \xe9n vagyok") -> "az llam n vagyok"[/color]

        The functions you want are str.encode and str.decode:
        "az llam n vagyok".encode( "string_escape" ) -> "az \xe1llam \xe9n
        vagyok"
        "az \xe1llam \xe9n vagyok".decode( "string_escape" ) -> "az llam n
        vagyok"

        If you choose to use Unicode strings instead, use the "unicode_escape "
        codec instead of the "string_esc ape" codec.

        A list of the standard encodings is available at
        http://docs.python.org/lib/node127.html if you need with some other
        format (rot13 is my personal favourite :P).

        Comment

        Working...