unicode 3 digit decimal conversion
Martin v. Löwis
martin at v.loewis.de
Sat Sep 27 06:23:35 EDT 2003
Rune Hansen <rune.hansen at viventus.no> writes:
> What I need is the converted string to read u'Gratis \248l' (*
> How do I do this without going through each and every character of the
> string?
You can register an error callback, like this:
import codecs
def decimal_escape(exc):
try:
data = exc.object
res = u""
for i in range(exc.start, exc.end):
char = ord(data[i])
if char < 1000:
res += u"\\%03d" % char
else:
# Unsupported character
raise exc
return res, exc.end
except:
raise exc
codecs.register_error("decimal-escape", decimal_escape)
print u"Gratis \xf8l".encode("us-ascii", "decimal-escape")
Notice That your specification is a bit unclear as to what to do with
characters > 1000; I assume they are not supported in your protocol.
Regards,
Martin
More information about the Python-list
mailing list