2013-08-27 84 views
-1

我在解析特定類型的XML時遇到困難。用Python解析XML忽略部分

XML文件看起來像:

<channels> 
    <genre type = blah1> 
     <channel name="Channel 1"> 
      <show> 
       <title>hello</title> 
      </show> 
     </channel> 
     <channel name="Channel 2"> 
      <show> 
       <title>hello</title> 
      </show> 
     </channel> 
    </genre> 
    <genre type="blah2"> 
     <channel name="Channel 3"> 
      <show> 
       <title>hello</title> 
      </show> 
     </channel> 
    </genre> 
</channels> 

所以我的問題如下:

channelList = rootElem.find(".//channel[@name]") 
howManyChannels = len(channelList) 


for x in range(1, howManyChannels): 
    print x 
    print rootElem.find(".//channel[@name]["+str(x)+"]").get('name') 
    for y in rootElem.find(".//channel[@name]["+str(x)+"]"): 
     print y.findtext('title') 

這得到與通道2個,然後錯誤:

Traceback (most recent call last): 
    File "parse.py", line 17, in <module> 
    print rootElem.find(".//channel[@name]["+str(x)+"]").get('name') 
AttributeError: 'NoneType' object has no attribute 'get' 

爲什麼沒有按't代碼:

for y in rootElem.find(".//channel[@name]["+str(x)+"]"): 

包含第3個頻道,爲什麼它被隔離,因爲它在另一個流派選項卡中?如何更改代碼以適應此?

我試圖在列表中存儲什麼渠道與什麼顯示。

更新: 我不明白爲什麼

channelList = rootElem.find(".//channel[@name][3]") 

產生錯誤連外循環。

url = 'myxmlurl.com/xml.xml' 
request = urllib2.Request(url, headers={"Accept" : "application/xml"}) 
u = urllib2.urlopen(request) 
tree = ElementTree.parse(u) 
rootElem = tree.getroot() 
+0

縮進問題。如果您的代碼沒有正確縮進,我們無法對您的代碼發表評論。 – Marcin

+0

請將代碼格式化爲代碼。 – Marcin

回答

0

首先,您發佈的代碼在語法上無效,因爲它沒有縮進。但是,問題的根源在於您正在使用range進行迭代。

相反的:

channelList = rootElem.find(".//channel[@name]") 
howManyChannels = len(channelList) 


for x in range(1, howManyChannels): 

務必:

channelList = rootElem.find(".//channel[@name]") 
for channel in channelList: 
    pass #whatever 

這樣,你不需要再爲頻道搜索。

此外,您的搜索「不起作用」,因爲沒有名稱爲"3"的通道元素。嘗試搜索"Channel 3"

+0

好的,但將代碼更改爲上面的代碼將不允許使用以下代碼:print rootElem.find(「.// channel [@name] [」+ str(x)+「]」)。get('name')'因爲x需要是一個數字而不是一個對象。對不起,縮進錯誤,這是我的錯在最初的帖子,代碼不反映這種方式。 – JavaWizKid

+0

@JavaWizKid你沒有讀過這一路?你不需要再次搜索頻道,因爲你就在那裏。 – Marcin

+0

好的謝謝你的循環修復。如果我只是在循環外面使用'channelList = rootElem.find(「.// channel [@name] [3]」)'並且打印它會給出錯誤,那麼這並不能解決問題。 – JavaWizKid