Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90014
| References | <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> |
|---|---|
| Date | 2015-05-06 10:02 +1000 |
| Subject | Re: Stripping unencodable characters from a string |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.152.1430870587.12865.python-list@python.org> (permalink) |
On Wed, May 6, 2015 at 4:19 AM, Paul Moore <p.f.moore@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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Stripping unencodable characters from a string Paul Moore <p.f.moore@gmail.com> - 2015-05-05 11:19 -0700
Re: Stripping unencodable characters from a string Dave Angel <davea@davea.name> - 2015-05-05 15:00 -0400
Re: Stripping unencodable characters from a string Paul Moore <p.f.moore@gmail.com> - 2015-05-05 12:24 -0700
Re: Stripping unencodable characters from a string Marko Rauhamaa <marko@pacujo.net> - 2015-05-05 22:55 +0300
Re: Stripping unencodable characters from a string Jon Ribbens <jon+usenet@unequivocal.co.uk> - 2015-05-05 19:33 +0000
Re: Stripping unencodable characters from a string Chris Angelico <rosuav@gmail.com> - 2015-05-06 10:02 +1000
Re: Stripping unencodable characters from a string Serhiy Storchaka <storchaka@gmail.com> - 2015-05-08 15:28 +0300
csiph-web