Convert Latin-1 to UTF-8
Fredrik Lundh
effbot at telia.com
Fri May 12 10:07:02 EDT 2000
<simon3422 at my-deja.com> wrote:
> Anybody who knows if there is a function in
> Python for converting Latin-1 to UTF-8?
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, "")
</F>
<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->
More information about the Python-list
mailing list