NarūnasK
NarūnasK

Reputation: 4950

TypeError: decoding str is not supported, when encoding is explicitly specified

Any idea why this is happening when encoding is explicitly specified?

In [23]: import sys

In [24]: sys.getdefaultencoding()
Out[24]: 'utf-8'

In [25]: str(b'', encoding='utf-8') == ''
Out[25]: True

In [26]: str('') == ''
Out[26]: True

In [27]: str('', encoding='utf-8') == ''
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-27-e187972042f8> in <module>()
----> 1 str('', encoding='utf-8') == ''

TypeError: decoding str is not supported

According to the docs of str:

encoding defaults to sys.getdefaultencoding()

Upvotes: 0

Views: 8808

Answers (1)

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85582

The help is pretty clear here:

str(object='') -> str

str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.

You can specify the encoding only for bytes (b'') not for strings such as ''.

Upvotes: 2

Related Questions