[Python-3000] Bytes to Unicode Conversion

Chris Rebert 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:
> Hello,
>
> 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
> unless
> 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")

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> Thanks for your help.
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe:
> http://mail.python.org/mailman/options/python-3000/cvrebert%40gmail.com
>
>


More information about the Python-3000 mailing list