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, "")