0

不完全确定如何处理此错误...

代码:

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.itertext('netmask')    
    for ipaddyElement in network.iterfind('ipaddr'):
        ipaddy = network.find('ipaddr')
    print (name.text,class_name.text,ipaddy.text,netmask,color.text)

错误:

builtins.TypeError: itertext() takes exactly 1 positional argument (2 given)
line 10, in <module>
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>

当然还有其他包含网络掩码的对象,我认为这是错误的来源,但是我认为 for 循环会纠正这一点。

我该如何解决?:)

4

2 回答 2

2

更改itertext( 'netmask')itertext()

文档显示它没有像您当前所做的那样接收附加参数。

文本文档

于 2013-03-25T18:36:10.440 回答
0

为什么不像在循环的其余部分中那样使用 .find 呢?

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)
于 2013-03-25T22:50:17.880 回答