unicode 3 digit decimal conversion

Rune Hansen rune.hansen at viventus.no
Sat Sep 27 06:45:20 EDT 2003


Hi,
The tip from Klaus "solved" my problem for the time beeing, but your 
snipplet definitively goes into my "tool chest"

thanks

regards

/rune

Martin v. Löwis wrote:
> 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