首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >intertext()采用精确的一个位置论证(2个给定)

intertext()采用精确的一个位置论证(2个给定)
EN

Stack Overflow用户
提问于 2013-03-25 18:33:25
回答 2查看 319关注 0票数 0

不知道该怎么处理这个错误..。

代码:

代码语言:javascript
复制
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)

错误:

代码语言:javascript
复制
builtins.TypeError: itertext() takes exactly 1 positional argument (2 given)
line 10, in <module>
netmask = network.itertext('netmask')

例如,XML本身:

代码语言:javascript
复制
<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循环会对此进行更正。

我如何解决这个问题?:)

EN

回答 2

Stack Overflow用户

发布于 2013-03-25 18:36:10

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

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

迭代文本文档

票数 2
EN

Stack Overflow用户

发布于 2013-03-25 22:50:17

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

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15622184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档