2013-03-25 57 views
0

不完全知道如何對付這個錯誤...互文性()恰恰1位置arguement(2給出)

代碼:

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循環會糾正這一點。

我該如何解決這個問題? :)

回答

2

更改itertext('netmask')itertext()

文檔顯示,它並沒有獲得額外的參數,因爲你正在做的事情。

itertext documentation

+0

如果我這樣做,我得到「builtins.NameError:名稱‘子網掩碼’沒有定義」 – Numpty 2013-03-25 18:39:19

+0

@Numpty然後你做別的東西來代替。 – wRAR 2013-03-25 18:40:30

+0

關注更具體?如果我去打印'網絡掩碼',它是不確定的。這可能是「修復」的一部分,但它不起作用 – Numpty 2013-03-25 18:43:12

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) 
+0

我以前就是這麼做的(正在嘗試一系列不同的做法)。網絡掩碼仍然是'未定義' – Numpty 2013-03-26 01:39:02

相關問題