unicode-to-ascii: replace with space, not "?"

Mark Tolonen metolone+gmane at gmail.com
Wed Oct 14 23:44:25 EDT 2009


"Allen Fowler" <allen.fowler at yahoo.com> wrote in message 
news:59796.73163.qm at web45608.mail.sp1.yahoo.com...
> Hello,
>
> I've been using "data.encode('ascii','replace')" to force an ASCII string 
> out of Unicode data, with "?" in the place of non-ASCII letters.
>
> However, now I want to use a blank space (or maybe a dash) instead of a 
> question mark.
>
> How do I do this?

See codecs.register_error().  Here's a simplistic example:

# coding: utf-8
import codecs

def handler(e):
    return (u'-',e.start + 1)

codecs.register_error('mine',handler)
s = u'My name is 马克.'
print s.encode('ascii','mine')

OUTPUT:
My name is --.


-Mark 





More information about the Python-list mailing list