Subject: Re: Convert Latin-1 to UTF-8 - DN [1]
"Fredrik Lundh" <effbot@telia.com> - 12 May 2000 - comp.lang.python
<simon3422@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
-->
Last modified
2000-07-20
2000-07-20
(195.108.246.52)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
