Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'encoding': 0.05; 'string': 0.09; "(i'd": 0.09; 'subject:characters': 0.09; 'subject:string': 0.09; 'python': 0.11; 'binary,': 0.16; 'encodes': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'file,': 0.19; 'seems': 0.21; '(in': 0.22; 'header:User-Agent:1': 0.23; 'of.': 0.24; 'skip:e 30': 0.24; 'specify': 0.24; 'unicode': 0.24; 'paul': 0.24; 'file.': 0.24; '(or': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'minor': 0.31; 'way?': 0.31; 'file': 0.32; 'probably': 0.32; 'open': 0.33; 'charge': 0.33; 'skip:d 20': 0.34; 'subject:from': 0.34; 'could': 0.34; "can't": 0.35; 'but': 0.35; 'dance': 0.36; 'two': 0.37; 'starting': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'new': 0.61; "you're": 0.61; 'charset:windows-1252': 0.65; 'benefit': 0.68; 'received:74.208': 0.68; '3.4': 0.84; 'received:74.208.4.194': 0.84 Date: Tue, 05 May 2015 15:00:38 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Stripping unencodable characters from a string References: <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> In-Reply-To: <24ef6c6d-a47a-4d8c-8651-c581e25161cb@googlegroups.com> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K0:2Rpd3Z+YUrj5WCiH5hagCcEk7rq7tmKYao5EUbLzydVobQQoHww q/RTinX+poyxPwg9tFIRS93pqjHDNm4ikiDRW+IRdsnovyt5qdUvy3/wVfMqV5K0xI4DmuB nCoFEcIj5+7JzucWupxPl03nBAyonM6jwarPODAI7pfczJYhFf3l++04w2BQxcsJ5ug3NmG tBKWj5piqDGA8wIzKFPFg== X-UI-Out-Filterresults: notjunk:1; X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430852452 news.xs4all.nl 2879 [2001:888:2000:d::a6]:45134 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89991 On 05/05/2015 02:19 PM, Paul Moore wrote: You need to specify that you're using Python 3.4 (or whichever) when starting a new thread. > 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. > > Thanks, > Paul. > If you're going to take charge of the encoding of the file, why not just open the file in binary, and do it all with file.write(data.encode( myencoding, errors='replace') ) i can't see the benefit of two encodes and a decode just to write a string to the file. Alternatively, there's probably a way to open the file using codecs.open(), and reassign it to sys.stdout. -- DaveA