Python3.3 str() bug?
jarausch at igpm.rwth-aachen.de
Fri Nov 9 12:08:14 CET 2012
On Fri, 09 Nov 2012 10:37:11 +0100, Stefan Behnel wrote:
> Helmut Jarausch, 09.11.2012 10:18:
>> probably I'm missing something.
>> Using str(Arg) works just fine if Arg is a list.
>> gives the error
>> TypeError: coercing to str: need bytes, bytearray or buffer-like object,
>> list found
>> If this isn't a bug how can I use str(Arg,encoding='latin-1') in general.
>> Do I need to flatten any data structure which is normally excepted by str() ?
> Funny idea to call this a bug in Python. What your code is asking for is to
> decode the object you pass in using the "latin-1" encoding. Since a list is
> not something that is "encoded", let alone in latin-1, you get an error,
> and actually a rather clear one.
> Note that this is not specific to Python3.3 or even 3.x. It's the same
> thing in Py2 when you call the equivalent unicode() function.
For me it's not funny, at all.
Whenever Python3 encounters a bytestring it needs an encoding to convert it to
a string. If I feed a list of bytestrings or a list of list of bytestrings to
'str' , etc, it should use the encoding for each bytestring component of the
given data structure.
How can I convert a data strucure of arbitrarily complex nature, which contains
bytestrings somewhere, to a string?
This problem has arisen while converting a working Python2 script to Python3.3.
Since Python2 doesn't have bytestrings it just works.
Tell me how to convert str(obj) from Python2 to Python3 if obj is an
arbitrarily complex data structure containing bytestrings somewhere
which have to be converted to strings with a given encoding?
More information about the Python-list