[Python-3000] Bytes to Unicode Conversion

Chris Rebert cvrebert at gmail.com
Sun Nov 16 21:20:45 CET 2008


Ah, my bad. Should never have referred to the Python 2.6 docs. :)

Replace "unicode" with "str" in my line of code and I think it should work.

Cheers,
Chris

On Sun, Nov 16, 2008 at 12:13 PM, Pb2Au <pb2au.alchemy at gmail.com> wrote:
> On Sun, Nov 16, 2008 at 4:31 PM, Chris Rebert <cvrebert 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.
>>>
>>> Thanks for your help.
>>
>>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
>
> I know that it had worked in the version 2.5, Python 3.0 rc2 doesn't
> seem to recognize it as a function.
>
> Python 3.0rc2 (r30rc2:67141, Nov  7 2008, 11:43:46) [MSC v.1500 32 bit
> (Intel)]
> on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> unicode()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'unicode' is not defined
>
>



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


More information about the Python-3000 mailing list