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

Mark Tolonen metolone+gmane at gmail.com
Thu Oct 15 05:44:25 CEST 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)

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

My name is --.


More information about the Python-list mailing list