Python - 描述符“split”需要“str”对象,但收到“unicode”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14098638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Descriptor 'split' requires a 'str' object but received a 'unicode'
提问by Tom Jenkins
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 received a 'unicode'
类型错误:描述符 'split' 需要一个 'str' 对象,但收到了一个 'unicode'
That's the whole def:
这就是整个定义:
def assemblePacket(self, type):
ipSplit = str.split(self.serverVars[0], '.')
packet = 'SAMP'
packet += chr(int(ipSplit[0]))
packet += chr(int(ipSplit[1]))
packet += chr(int(ipSplit[2]))
packet += chr(int(ipSplit[3]))
packet += chr(self.serverVars[1] & 0xFF)
packet += chr(self.serverVars[1] >> 8 & 0xFF)
packet += type
return packet
And here is the problem:
这是问题所在:
ipSplit = str.split(self.serverVars[0], '.')
I'm sure it's not because of the code, I've tried it before (the same script) and it worked. No idea why it doesn't now.And this "unicode" makes me think I have to change "str.split", but hmmm. Waiting for opinions :)
我确定这不是因为代码,我之前尝试过(相同的脚本)并且它有效。不知道为什么现在没有。这个“unicode”让我觉得我必须改变“str.split”,但是嗯。等待意见:)
采纳答案by Abe Karplus
The problem is that str.splitis a method of the strclass, but is being called for an object of the unicodeclass. Call the method directly with ipSplit = self.serverVars[0].split('.')to have it work for anything (including strand unicode) with a splitmethod.
问题是它str.split是str类的一个方法,但正在为unicode类的对象调用。直接使用 with 调用该方法ipSplit = self.serverVars[0].split('.')以使其适用于带有方法的任何内容(包括str和unicode)split。
回答by Abhijit
As @Abe mentioned, the problem here is, you are using str.splitto split an object of type unicodewhich is causing the failure.
正如@Abe 所提到的,这里的问题是,您正在使用str.split来拆分unicode导致失败的类型对象。
There are three options for you
为您提供三种选择
- 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. - You can also call
unicode.split(). This will work well forunicodestring but fornon-unicodestring, this will fail again. - Finally, you can import the stringmodule and call the string.splitfunction. This function converts the
split()function call to method call thus enabling you to transparently call thesplit()irrespective if the object type. This is beneficial when you are using thesplit()as callbacks esp to functions likemap()
- 在这种特殊情况下,您可以简单地调用
split()对象的方法。这将确保无论对象 (str,unicode)的类型如何,方法调用都会正确处理它。 - 您也可以调用
unicode.split(). 这将适用于unicode字符串,但对于non-unicode字符串,这将再次失败。 - 最后,您可以导入string模块并调用string.split函数。此函数将
split()函数调用转换为方法调用,从而使您能够透明地调用split()对象类型。当您将split()as 回调 esp 用于诸如map()
回答by serv-inc
Neither method worked when using isdigit. If you are in a similar solution, you could try a try-exceptblock similar to
使用isdigit. 如果您在类似的解决方案中,您可以尝试类似的try-except块
try:
output += filter(str.isdigit, some_string)
except TypeError:
output += filter(unicode.isdigit, some_string)

