How do I encode and decode this data to write to a file?
Ned Batchelder
ned at nedbatchelder.com
Wed May 1 18:01:39 EDT 2013
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
More information about the Python-list
mailing list