Python - Descriptor 'split' requires a 'str' object but received a 'unicode'

后端 未结 3 2146
小鲜肉
小鲜肉 2021-02-20 14:26

Erm, I have ready-to-use code, and I\'m sure it really works, but I get the following error:

TypeError: descriptor \'split\' requires a \'str\' object but

3条回答
  •  死守一世寂寞
    2021-02-20 14:32

    As @Abe mentioned, the problem here is, you are using str.split to split an object of type unicode which is causing the failure.

    There are three options for you

    1. In this particular case, you can simply call the split() method for the object. This will ensure that irrespective of the type of the object (str, unicode), the method call would handle it properly.
    2. You can also call unicode.split(). This will work well for unicode string but for non-unicode string, this will fail again.
    3. Finally, you can import the string module and call the string.split function. This function converts the split() function call to method call thus enabling you to transparently call the split() irrespective if the object type. This is beneficial when you are using the split() as callbacks esp to functions like map()

提交回复
热议问题