【问题标题】:ET.find() takes exactly 2 arguments (3 given)ET.find() 正好需要 2 个参数(给定 3 个)
【发布时间】:2017-08-25 14:02:53
【问题描述】:

我是 Python 新手,遇到错误

ET.find() 只接受 2 个参数(给定 3 个)

在执行期间

import xml.etree.ElementTree as ET

ns = {'conv_svc': 'http://schemas.com/serviceconvert'}            
jobTypesXml = self.__server_request(url, None)    
root = ET.fromstring(jobTypesXml)    
for job in root.find('conv_svc:GetJobTypesResult', ns):

我的第一个问题是:下面的初始化推导出什么类型?

ns = {'conv_svc': 'http://schemas.com/serviceconvert'}            

回答这个问题我可以更进一步自己找出错误! 提前致谢!

【问题讨论】:

    标签: python elementtree xml.etree


    【解决方案1】:

    find() 方法only takes a single argument 所以你不能做任何你正在尝试的事情(至少不能用find())。

    为了回答您的问题,{'conv_svc': 'http://schemas.com/serviceconvert'} 是一个字典,将被解释为单个参数。如果您想知道为什么错误表明您正在传递 3 个参数(您没有传递),那是因为 self 也被视为类方法的参数。

    class Testing(object):
    
        def __init__(self):
            self.a = 2
    
        def do_something(self, b):
            self.a += b
    
    obj = Testing()
    obj.do_something(2, 3) # Clearly passing only 2 arguments
    

    给予:

    TypeError: do_something() takes exactly 2 arguments (3 given)
    

    编辑

    感谢@ShreyashSSarnayak 指出find() 可以接受an optional extra argument in Python 3。错误消息确认您使用的是 Python 2,但可能正在阅读一些与 Python 3 相关的文档。

    【讨论】:

    • 在python3中需要two arguments
    • @ShreyashSSarnayak 啊哈,所以错误是他们正在使用 Python 2 和 Python 3 文档(问题中发布的错误证明他们无法传递可选参数)。我会编辑,谢谢。
    猜你喜欢
    • 2016-09-01
    • 2012-03-15
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 2013-04-07
    相关资源
    最近更新 更多