[Python-3000] Bytes to Unicode Conversion
cvrebert at gmail.com
Sun Nov 16 19:31:22 CET 2008
On Sat, Nov 15, 2008 at 8:46 AM, Pb2Au <pb2au.alchemy at gmail.com> wrote:
> I recently changed from Python 2.5 to Python 3.0 rc2, and have
> been trying to find out how to convert byte strings (b"example")
> to unicode strings ("example"). I noticed that some of these had
> changed in the latest version.
> One reason for a conversion between the two is the urllib.request.urlopen()
> feature, which requires the string to be unicode rather than bytes, or else
> you would receive an AttributeError error about 'bytes' object having no
> attribute 'timeout'. The read() attribute of the urllib.request.urlopen()
> function returns a byte string, which means I can't parse for information
> in the bytes string to use in a second urllib.request.urlopen() function
> it was to be converted to unicode first.
> Am I simply overlooking something, or is there a built in function for
> converting bytes to unicode? It seems like a function could be created
> pretty easily if it has already not, but there isn't much sense in
> reinventing the wheel if the function is already there.
Already exists. Has for quite a while now:
the_unicode = unicode(some_bytes, "name of encoding")
Follow the path of the Iguana...
> Thanks for your help.
> Python-3000 mailing list
> Python-3000 at python.org
More information about the Python-3000