Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90158
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'output': 0.05; 'string': 0.09; "(i'd": 0.09; 'absent': 0.09; 'implements': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:characters': 0.09; 'subject:string': 0.09; "'w',": 0.16; '(general': 0.16; 'file-like': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'superfluous': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'solution.': 0.20; 'seems': 0.21; '(in': 0.22; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'of.': 0.24; 'skip:e 30': 0.24; 'unicode': 0.24; 'paul': 0.24; "i've": 0.25; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'minor': 0.31; 'way?': 0.31; 'file': 0.32; 'probably': 0.32; 'cases': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'could': 0.34; 'problem': 0.35; 'but': 0.35; 'there': 0.35; 'dance': 0.36; 'object,': 0.36; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'simple': 0.61; 'charset:windows-1252': 0.65; 'approach.': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Serhiy Storchaka <storchaka@gmail.com> |
| Subject | Re: Stripping unencodable characters from a string |
| Date | Fri, 08 May 2015 15:28:54 +0300 |
| References | <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | 193.202.118.166 |
| User-Agent | Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
| In-Reply-To | <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.243.1431088153.12865.python-list@python.org> (permalink) |
| Lines | 24 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1431088153 news.xs4all.nl 2827 [2001:888:2000:d::a6]:43055 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:90158 |
Show key headers only | View raw
On 05.05.15 21:19, Paul Moore 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. There are flaws in this approach. 1) file.encoding can be None (StringIO) or absent (general file-like object, that implements only write()). 2) When the encoding is UTF-16, UTF-32, UTF-8-SIG, the output will contain superfluous byte order marks. This is not easy problem and there is no simple solution. In particular cases you can create TextIOWrapper(file.buffer, 'w', encoding=file.encoding, errors='replace', newline=file.newlines, write_through=True) and write to it, but be aware of limitations.
Back to comp.lang.python | Previous | Next — Previous 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