【问题标题】:Python error - TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'Python错误-TypeError:+的不支持的操作数类型:'NoneType'和'str'
【发布时间】:2015-06-18 00:29:33
【问题描述】:

我正在尝试调试现有脚本,但在运行脚本时出现以下错误。 ./check_tandbergvideo CE s 10.50.174.138 此脚本尝试检查端点是否已注册并返回状态。

Traceback(最近一次通话最后一次):文件“./check_tandbergvideo”, 第 156 行,在 main() 文件“./check_tandbergvideo”中,第 114 行, 在主 EP = getXML(sys.argv[3],sys.argv[1]) 文件中 “./check_tandbergvideo”,第 79 行,在 getXML H323Status = getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"状态") + "。错误:" + getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"原因")
类型错误:+ 不支持的操作数类型:“NoneType”和“str”

这是引发错误的代码部分。

if model == "CE":
  # SIPStatus =  getElement(tree,xml2+"SIP/"+xml2+"Registration/"+xml2+"Status") + ". Errors: " + getElement(tree,xml2+"SIP/"+xml2+"Registration/"+xml2+"Reason")
    SIPStatus =  str(getElement(tree,xml2+"SIP/"+xml2+"Profile/"+xml2+"Registration/"+xml2+"Status")) + ". Errors: " + str(getElement(tree,xml2+"SIP/"+xml2+"Profile/"+xml2+"Registration/"+xml2+"Reason"))
    H323Status = getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"Status") + ". Errors: " + getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"Reason")
    ReleaseKey =   getElement(tree,xml2+"SystemUnit/"+xml2+"Software/"+xml2+"ReleaseKey")
    EPModel = getElement(tree,xml2+"SystemUnit/"+xml2+"ProductId")
SWVer =getElement(tree,xml2+"SystemUnit/"+xml2+"Software/"+xml2+"Version")
    else:
    badSyntax()
EPData = {"Model":EPModel,"SIP":SIPStatus,"H323":H323Status,"RK":ReleaseKey,"SW":SWVer}
return(EPData)

能否请您验证代码H323的第二行语法是否正确?

【问题讨论】:

  • 您的一个getElement 正在返回None。进行一些调试以找出它是哪一个。
  • 检查您的 getElement() 参数,其中一个是导致问题的“NoneType”类型。
  • 而有问题的元素是xml2(它是唯一添加了字符串的元素)。
  • 是的....确实,您可以发布与xml2相关的代码
  • 特别有可能getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"Status") 正在返回None。解决这个问题,你应该会很好。

标签: python


【解决方案1】:

这不是您的脚本中的错误。它只是说TypeError: unsupported operand type(s) for +: 'NoneType' and 'str',基本上意味着它不能结合None"string"。你应该做str(None)+"string"或设置一些条件if s == None: do something来避免。

【讨论】:

  • 我强烈建议不要使用str() 强制转换方法,因为这会在其他地方创建垃圾数据、导致错误或在其他地方创建垃圾数据(甚至可能损坏现有数据)。
  • 如果你想给None添加一个字符串,这就是它的工作原理。你知道其他方法吗?告诉我。
  • 您到底为什么要向None 添加一个字符串?我认为这显然是一种不受欢迎的行为。您只是安抚错误消息而不是解决问题。
  • 你不需要做 str(None) 来做if s == None: do something。 str(None) != None,两个不同的东西,不能混淆或互换。
  • 对于那些不知道的人。 “无”是没有值。这不是错误。您可以将其记录为“无”或“”或完全跳过它。你将如何处理它取决于你。如果您尝试将 None 添加到字符串,您将得到的错误。
【解决方案2】:

非常感谢大家的宝贵意见...我试过了,它奏效了。

H323Status = str(getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"Status")) + ". Errors: " + str(getElement(tree,xml2+"H323/"+xml2+"Gatekeeper/"+xml2+"Reason"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-24
    • 2014-06-15
    • 2018-07-07
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    相关资源
    最近更新 更多