Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python intertext()正好接受1个位置参数(给定2个)_Python_Xml - Fatal编程技术网

Python intertext()正好接受1个位置参数(给定2个)

Python intertext()正好接受1个位置参数(给定2个),python,xml,Python,Xml,不确定如何处理此错误 代码: 错误: builtins.TypeError: itertext() takes exactly 1 positional argument (2 given) line 10, in <module> netmask = network.itertext('netmask') builtins.TypeError:itertext()正好接受1个位置参数(给定2个) 第10行,在 netmask=network.itertext('netmask')

不确定如何处理此错误

代码:

错误:

builtins.TypeError: itertext() takes exactly 1 positional argument (2 given)
line 10, in <module>
netmask = network.itertext('netmask')
builtins.TypeError:itertext()正好接受1个位置参数(给定2个)
第10行,在
netmask=network.itertext('netmask')
例如,XML本身:

<network_objects>
<network_object>
<Name>Internal-192.168.112.0_24b</Name>
<Class_Name>network</Class_Name>
<add_adtr_rule>false</add_adtr_rule>
<broadcast><![CDATA[allow]]></broadcast>
<color><![CDATA[dark orchid]]></color>
<comments><![CDATA[no comment]]></comments>
<edges/>
<ipaddr><![CDATA[192.168.112.0]]></ipaddr>
<location><![CDATA[internal]]></location>
<location_desc><![CDATA[]]></location_desc>
<netmask><![CDATA[255.255.255.0]]></netmask>
<type><![CDATA[network]]></type>
</network_object>
</network_objects>

内部-192.168.112.0_24b
网络
假的
当然还有其他不包含netmask的对象,我假设这就是错误的来源,但是我假设for循环会对此进行纠正

如何解决此问题?:)

itertext('netmask')
更改为
itertext()

文档显示,它没有像您当前所做的那样接收额外的参数


为什么不直接使用。在循环的其余部分找到你所拥有的

import xml.etree.ElementTree as ET
tree = ET.parse('network_objects.xml')
root = tree.getroot()

for network in root.iterfind('network_object'):
    name = network.find('Name')
    class_name = network.find('Class_Name')
    color = network.find('color')
    for netmaskElement in network.iterfind('netmask'):
        netmask = network.find('netmask')    
    for ipaddyElement in network.iterfind('ipaddr'):
        ipaddy = network.find('ipaddr')
    print (name.text,class_name.text,ipaddy.text,netmask,color.text)

如果我这样做,我会得到'builtins.NameError:name'netmask'未定义'@Numpty,那么您做了其他操作。要更具体一些吗?如果我去打印“网络掩码”,它是未定义的。这可能是“修复”的一部分,但如果您在for循环内部定义
netmask
,它就不起作用了。确保实际输入了此for循环
用于网络中的网络掩码元素。iterfind('netmask'):
如果未输入for循环,则永远不会定义
网络掩码
。@Numpty您似乎混淆了
网络掩码
网络掩码'
。例如,我之前做得很好(尝试了一系列不同的方法)。网络掩码最终仍然是“未定义的”
import xml.etree.ElementTree as ET
tree = ET.parse('network_objects.xml')
root = tree.getroot()

for network in root.iterfind('network_object'):
    name = network.find('Name')
    class_name = network.find('Class_Name')
    color = network.find('color')
    for netmaskElement in network.iterfind('netmask'):
        netmask = network.find('netmask')    
    for ipaddyElement in network.iterfind('ipaddr'):
        ipaddy = network.find('ipaddr')
    print (name.text,class_name.text,ipaddy.text,netmask,color.text)