Stripping unencodable characters from a string
Jon Ribbens
jon+usenet at unequivocal.co.uk
Tue May 5 15:33:36 EDT 2015
On 2015-05-05, 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.
Perhaps something like one of:
file.buffer.write(data.encode(file.encoding, errors="replace"))
or:
sys.stdout = io.TextIOWrapper(sys.stdout.detach(),
encoding=sys.stdout.encoding, errors="replace")
(both of which could go wrong in various ways depending on your
circumstances).
More information about the Python-list
mailing list