Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90014
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; '(using': 0.07; 'string': 0.09; "(i'd": 0.09; 'subject:characters': 0.09; 'subject:string': 0.09; 'cc:addr:python-list': 0.11; 'backslash': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'simplest': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'seems': 0.21; '(in': 0.22; 'cc:addr:python.org': 0.22; 'of.': 0.24; 'skip:e 30': 0.24; 'string,': 0.24; 'unicode': 0.24; 'paul': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'minor': 0.31; 'way?': 0.31; 'file': 0.32; 'probably': 0.32; 'text': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'dance': 0.36; 'rather': 0.38; 'safe': 0.72; 'goal': 0.75; '2015': 0.84; 'to:none': 0.92 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=gl2ww+hrzAlp+N444mnReRDtgeEj9meLbJ/TvYaFXYc=; b=gLx5XpQCD/qVGaT2JLLXWG4UJa3khtITM2uo2lrQyZYtzMfdrBM5gxNNoc5vVJZb+6 3iiA3bR/qtubgR6gL2uKdbKgM5QukryFV5jEw2tYzCAgbChac86iXwzzR5/u4b24Ca51 lriunpTePeQxqXoBfNRw7Fpu6Ja7eaw6MtAEyq6FDM4JHsLtIgT0++mvYJcOB8eeUOax PNVtEdABrC99bFoQ0Rgp6tagouowPm7qoeel1drOfTW0vv5UrlrLmHP0n/vTK7uwbOln j3lVm0L8joghBxf768ZRspjUNy41SQZHiR4nUtQkexpSG4su8mRZrFGF8ZmtNVo6OQF6 p3zQ== |
| MIME-Version | 1.0 |
| X-Received | by 10.107.134.206 with SMTP id q75mr1516814ioi.27.1430870578663; Tue, 05 May 2015 17:02:58 -0700 (PDT) |
| In-Reply-To | <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> |
| References | <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> |
| Date | Wed, 6 May 2015 10:02:58 +1000 |
| Subject | Re: Stripping unencodable characters from a string |
| From | Chris Angelico <rosuav@gmail.com> |
| Cc | "python-list@python.org" <python-list@python.org> |
| Content-Type | text/plain; charset=UTF-8 |
| 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.152.1430870587.12865.python-list@python.org> (permalink) |
| Lines | 16 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1430870587 news.xs4all.nl 2944 [2001:888:2000:d::a6]:60606 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:90014 |
Show key headers only | View raw
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