不知道该怎么处理这个错误..。
代码:
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循环会对此进行更正。
我如何解决这个问题?:)
发布于 2013-03-25 18:36:10
将itertext( 'netmask')更改为itertext()
文档显示,它不会像您当前所做的那样接收额外的参数。
迭代文本文档
发布于 2013-03-25 22:50:17
为什么不像在循环的其余部分那样使用.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)https://stackoverflow.com/questions/15622184
复制相似问题