faqts : Computers : Programming : Languages : Python : Common Problems

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 20 people (80%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

Is there a function in Python for converting Latin-1 to UTF-8?

May 13th, 2000 00:01
unknown unknown, Fredrik Lundh


Here's one way to do it (well, two ways, actually):

import string, sys

if sys.version[:3] >= "1.6":
    def utf8(str):
        return unicode(str, "iso-8859-1").encode("utf-8")
else:
    # brute force translation from latin 1 to utf 8
    def utf8(str):
        out = []
        append = out.append
        for ch in str:
            if ch < "\200":
                append(ch)
            else:
                ch = ord(ch)
                append(chr(0xc0 | (ch >> 6)))
                append(chr(0x80 | (ch & 0x3f)))
        return string.join(out, "")