[Python-3000] encode function errors="replace", but print() failed, is this a bug?

Benjamin Peterson musiccomposition at gmail.com
Thu Nov 20 04:50:55 CET 2008


2008/11/19 Decheng Fan <fandecheng at gmail.com>:
> Hi,
>
> Recently I encountered a problem with the str.encode() function.  I used the
> function like this: s.encode("mbcs", "replace"), expecting it will eliminate
> all invalid characters.  However it failed with the following message:
> UnicodeEncodeError: 'gbk' codec can't encode character '\ue104' in position
> 4: i
>
> Am I using it in a wrong way or is it a bug?

print() sends it's data to stdout which encodes the data based on it's
own encoding. If you want to change this behavior, replace sys.stdout
with your own io.TextIOWrapper with 'replace' as the errors argument.


-- 
Cheers,
Benjamin Peterson
"There's nothing quite as beautiful as an oboe... except a chicken
stuck in a vacuum cleaner."


More information about the Python-3000 mailing list