Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44604
| 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 | <ned@nedbatchelder.com> |
| 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 <ned@nedbatchelder.com> |
| 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 <tony@tiger.invalid> |
| Subject | Re: How do I encode and decode this data to write to a file? |
| References | <27s15a-943.ln1@chris.zbmc.eu> <Zridnb6s37SKGhzMnZ2dnUVZ8u6dnZ2d@giganews.com> |
| In-Reply-To | <Zridnb6s37SKGhzMnZ2dnUVZ8u6dnZ2d@giganews.com> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1224.1367445706.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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='<html><head><title>Blah</title><body>éåäö</body></html>'
>>>> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 10:47 +0100
Re: How do I encode and decode this data to write to a file? Andrew Berg <bahamutzero8825@gmail.com> - 2013-04-29 05:11 -0500
Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 13:50 +0100
Re: How do I encode and decode this data to write to a file? Peter Otten <__peter__@web.de> - 2013-04-29 12:33 +0200
Re: How do I encode and decode this data to write to a file? Dave Angel <davea@davea.name> - 2013-04-29 07:46 -0400
Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 13:59 +0100
Re: How do I encode and decode this data to write to a file? Robert Kern <robert.kern@gmail.com> - 2013-04-29 14:11 +0100
Re: How do I encode and decode this data to write to a file? cl@isbd.net - 2013-04-29 15:38 +0100
Re: How do I encode and decode this data to write to a file? Skip Montanaro <skip@pobox.com> - 2013-04-29 09:56 -0500
Re: How do I encode and decode this data to write to a file? Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-29 18:02 -0400
Re: How do I encode and decode this data to write to a file? Tony the Tiger <tony@tiger.invalid> - 2013-05-01 16:20 -0500
Re: How do I encode and decode this data to write to a file? Ned Batchelder <ned@nedbatchelder.com> - 2013-05-01 18:01 -0400
Re: How do I encode and decode this data to write to a file? Ned Batchelder <ned@nedbatchelder.com> - 2013-05-01 19:36 -0400
csiph-web