Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44604
| Date | 2013-05-01 18:01 -0400 |
|---|---|
| From | Ned Batchelder <ned@nedbatchelder.com> |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1224.1367445706.3114.python-list@python.org> (permalink) |
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