Stripping unencodable characters from a string

Chris Angelico rosuav at gmail.com
Tue May 5 20:02:58 EDT 2015


On Wed, May 6, 2015 at 4:19 AM, Paul  Moore <p.f.moore at gmail.com> wrote:
> I want to write a string to an already-open file (sys.stdout, typically). However, I *don't* want encoding errors, and the string could be arbitrary Unicode (in theory). The best way I've found is
>
>     data = data.encode(file.encoding, errors='replace').decode(file.encoding)
>     file.write(data)
>
> (I'd probably use backslashreplace rather than replace, but that's a minor point).
>
> Is that the best way? The multiple re-encoding dance seems a bit clumsy, but it was the best I could think of.

The simplest solution would be to call ascii() on the string, which
will give you an ASCII-only representation (using backslash escapes).
If your goal is to write Unicode text to a log file in some safe way,
this is what I would be doing.

ChrisA



More information about the Python-list mailing list