Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:file': 0.07; 'utf-8': 0.07; 'f.close()': 0.09; 'subject:How': 0.10; 'cc:addr :python-list': 0.11; 'python': 0.11; '"w")': 0.16; "'w')": 0.16; '2.7.3': 0.16; 'file.close()': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.18; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'mon,': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; '+0100,': 0.31; '>>>>': 0.31; 'file': 0.32; 'raw': 0.33; 'there': 0.35; 'skip:f 40': 0.36; 'subject:data': 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'pm,': 0.38; 'more': 0.64; 'skip:f 50': 0.65; 'subject:this': 0.83; '8bit%:42': 0.95; '2013': 0.98 Date: Wed, 01 May 2013 18:01:39 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: Tony the Tiger Subject: Re: How do I encode and decode this data to write to a file? References: <27s15a-943.ln1@chris.zbmc.eu> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1367445706 news.xs4all.nl 15978 [2001:888:2000:d::a6]:48726 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44604 On 5/1/2013 5:20 PM, Tony the Tiger wrote: > On Mon, 29 Apr 2013 10:47:46 +0100, cl wrote: > >> raw = os.path.join(directory, self.getNameNoExtension()) + >> ".html" >> file = open(raw, "w") >> file.write("".join(html).encode('utf-8')) >> file.close() > This works for me: > > Python 2.7.3 (default, Aug 1 2012, 05:16:07) > [GCC 4.6.3] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> html='Blahéåäö' >>>> f=open('test.html', 'w') >>>> f.write(''.join(html.decode('utf-8').encode('utf-8'))) >>>> f.close() > Perhaps there are better ways to do it. Your .write() line is exactly equivalent to: f.write(html) Because: if X is a UTF-8 bytestring, then: X.decode('utf-8').encode('utf-8') == X And if X is a bytestring, then: ''.join(X) == X --Ned. > > /Grrr