[Tutor] Encoding

python at bdurham.com python at bdurham.com
Sun Mar 7 15:33:32 CET 2010


> Or, maybe even better, the format could be given as third parameter of file open(); then any read or write operation would directly convert from/to the said format. What do you all think?

See the codecs.open() command as an alternative to open().

With all the hassles of encoding, I'm puzzled why anyone would use the
regular open() for anything but binary operations.

Malcolm



----- Original message -----
From: "spir" <denis.spir at gmail.com>
To: "Python tutor" <tutor at python.org>
Date: Sun, 7 Mar 2010 14:29:11 +0100
Subject: Re: [Tutor] Encoding

On Sun, 7 Mar 2010 13:23:12 +0100
Giorgio <anothernetfellow at gmail.com> wrote:

> One more question: Amazon SimpleDB only accepts UTF8.
[...]
> filestream = file.read()
> filetoput = filestream.encode('utf-8')

No! What is the content of the file? Do you think it can be a pure
python representation of a unicode text?

uContent = inFile.read().decode(***format***)
<process, if any>
outFile.write(uContent.encode('utf-8'))

input -->decode--> process -->encode--> output

This gives me an idea: when working with unicode, it would be cool to
have an optional format parameter for file.read() and write. So, the
above would be:

uContent = inFile.read(***format***)
<process, if any>
outFile.write(uContent, 'utf-8')

Or, maybe even better, the format could be given as third parameter of
file open(); then any read or write operation would directly convert
from/to the said format. What do you all think?


denis
-- 
________________________________

la vita e estrany

spir.wikidot.com

_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list