Python 2.1 and Unicode

Alex Martelli aleaxit at yahoo.com
Wed Jan 31 03:00:46 EST 2001


"Dale Strickland-Clark" <dale at out-think.NOSPAMco.uk> wrote in message
news:hpse7tc1h31hosvt4ojeu419c531mm76ij at 4ax.com...
    [snip]
> We need two important features:
>
> 1. A print command that defaults to ignoring Unicode conversion errors -
or at least
> has the option to do so.

Having print be a _little_ bit flexible in how it stringifies its
arguments would be a big plus for us addicts of q&d-print-based
debugging, no argument about that.  Other 'stringifications', such
as the one performed by %-formatting (with a %s format code) etc,
may have similar issues.

> 2. A function, such as str() that converts *anything* to an ASCII string -
> ignoring Unicode errors.

You may put something like this in your site-configuration script[s]:

import __builtin__
def astr(x,str=__builtin__.str):
    try: return str(x)
    except UnicodeError:
        return x.encode('latin1','ignore')
__builtin__.str = astr

to replace the built-in str with a "more forgiving" one to your
taste.  This won't help with other implicit stringifications,
though -- for those, setting the default encoding (again in your
site configuration) may be best, but I don't think that, this
way, you can specify 'ignore' rather than 'strict' as the default.


Alex






More information about the Python-list mailing list