converting octal strings to unicode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • flamingivanova@gmail.com

    #1

    converting octal strings to unicode

    I have several ascii files that contain '\ooo' strings which represent
    the octal value for a character. I want to convert these files to
    unicode, and I came up with the following script. But it seems to me
    that there must be a much simpler way to do it. Could someone more
    experienced suggest some improvements?

    I want to convert a file eg. containing:

    hello \326du

    with the unicode file containing:

    hello Ödu


    ----------8<---------------------------------------
    #!/usr/bin/python

    import re, string, sys

    if len(sys.argv) > 1:
    file = open(sys.argv[1],'r')
    lines = file.readlines( )
    file.close()
    else:
    print "give a filename"
    sys.exit()

    def to_unichr(str):
    oct = string.atoi(str .group(1),8)
    return unichr(oct)

    for line in lines:
    line = string.rstrip(u nicode(line,'La tin-1'))
    if re.compile(r'\\ \d\d\d').search (line):
    line = re.sub(r'\\(\d\ d\d)', to_unichr, line)
    line = line.encode('ut f-8')
    print line

    ----------8<---------------------------------------

  • Christos TZOTZIOY Georgiou

    #2
    Re: converting octal strings to unicode

    On 23 Dec 2004 18:41:57 -0800, rumours say that flamingivanova@ gmail.com
    might have written:
    [color=blue]
    >I have several ascii files that contain '\ooo' strings which represent
    >the octal value for a character. I want to convert these files to
    >unicode, and I came up with the following script. But it seems to me
    >that there must be a much simpler way to do it. Could someone more
    >experienced suggest some improvements?[/color]

    decoded_string = "\326du".decode ("string_escape ")
    unicode_text = unicode(decoded _string, "latin-1")
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...

    Comment

    • Christos TZOTZIOY Georgiou

      #3
      Re: converting octal strings to unicode

      On 23 Dec 2004 18:41:57 -0800, rumours say that flamingivanova@ gmail.com
      might have written:
      [color=blue]
      >I have several ascii files that contain '\ooo' strings which represent
      >the octal value for a character. I want to convert these files to
      >unicode, and I came up with the following script. But it seems to me
      >that there must be a much simpler way to do it. Could someone more
      >experienced suggest some improvements?[/color]

      (hope I cancelled the previous off-by-one-backslash post...)

      your_string = "\\326du"
      decoded_string = your_string.dec ode("string_esc ape")
      unicode_text = unicode(decoded _string, "latin-1")
      --
      TZOTZIOY, I speak England very best.
      "Be strict when sending and tolerant when receiving." (from RFC1958)
      I really should keep that in mind when talking with people, actually...

      Comment

      Working...