[英]parse a xml file in python which nested tags are there
我只想解析一個xml文件,就像
<?xml version="1.0" encoding="UTF-8"?><Significant Major="3" Minor="0" Revision="1" xmlns="urn:reuterscompanycontent:significantdevelopments03"><RepNo>0091N</RepNo><CompanyName Type="Primary">XYZ</CompanyName><Production Date="2017-02-23T18:10:39" /><Developments><Development ID="3534388"><Dates><Source>2017-02-23T18:18:32</Source><Initiation>2017-02-23T18:18:32</Initiation><LastUpdate>2017-02-23T18:23:26</LastUpdate></Dates><Flags><FrontPage>0</FrontPage><Significance>1</Significance></Flags><Topics><Topic1 Code="254">Regulatory / Company Investigation</Topic1></Topics><Headline>FTC approves final order settling charges for Abbott's deal with St. Jude Medical</Headline></Development></Developments></Significant>
我只想解析Development標記並解析其每個嵌套的標記,而我的代碼如下:
import xml.etree.cElementTree as ET
tree = ET.ElementTree(file='../rawdata/SigDev_0091N.xml')
#get the root element
root = tree.getroot()
#print root.tag, root.attrib
for child in root:
#print child.tag, child.attrib
name = child.tag
print name
print 'at line 13'
if name is 'Developments':
print 'at line 15'
for devChild in name['Developments']:
print devChild.tag,devChild.attrib
它不在if塊內,我不知道為什么?
檢查name is 'Developments'
總是返回false
因為child.tag
以{xmlns}tagname
格式返回值。
對於您的情況:
名稱= {ur:reuterscompanycontent:significantdevelopments03}發展
您可以參考這個問題的答案。
簡單的字符串方法strip()
, find()
, split()
或re
可以幫助您跳過名稱空間進行比較。
與Python相關的文檔: https : //docs.python.org/2/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.