[英]elementtree: get the contents of specific tags in xml document
我試圖在XML文件中提取特定標簽的內容。
示例XML:
<facts>
<fact>
<name>crash</name>
<full_name>Crash</full_name>
<variables>
<variable>
<name>id</name>
<proper_name>Crash Instance</proper_name>
<type>INT</type>
<interpretation>key</interpretation>
</variable>
<variable>
<name>accident_key</name>
<proper_name>Case Identifier</proper_name>
<interpretation>string</interpretation>
<type>CHAR(9)</type>
</variable>
<variable>
<name>accident_year</name>
<proper_name>Crash Year</proper_name>
<interpretation>dim</interpretation>
<type>INT</type>
</variable>
</variables>
</fact>
<fact>
<name>vehicle</name>
<full_name>Vehicle</full_name>
<variables>
<variable>
<name>id</name>
<proper_name>Vehicle Instance</proper_name>
<type>INT</type>
</variable>
<variable>
<name>crash_id</name>
<proper_name>Crash Instance</proper_name>
<type>INT</type>
</variable>
</variables>
</fact>
</facts>
我想從節點中提取標簽的所有內容,但僅限於Crash事實。
到目前為止,這是我的代碼。
def header(filename, fact):
lst = []
tree = ET.parse(filename) #read in the XML
for fact in tree.iter(tag = 'fact'):
factname = fact.find('name').text
if factname == fact: #choose the fact to pull from
for var in fact.iter(tag = 'variable'):
name = var.find('name').text
lst.append(name)
return lst #return a list of all the <name> tags from the Crash fact
newlst = header('schema.xml','crash')
我的輸出newlst應該是Crash事實中所有標簽的列表。 但它一直空着。
奇怪的是,如果我對所有內容進行硬編碼(並刪除函數),它會返回正確的輸出:
lst = []
tree = ET.parse('schema.xml')
for fact in tree.iter(tag = 'fact'):
factname = fact.find('name').text
if factname == 'crash':
for var in fact.iter(tag = 'variable'):
name = var.find('name').text
lst.append(name)
print(lst)
Output: ['id',
'accident_key',
'accident_year']
在函數中,您將變量fact
用作參數,並將第一個for
循環變量。 試試這個版本:
def header(filename, target_factname):
lst = []
tree = ET.parse(filename) #read in the XML
for fact in tree.iter(tag = 'fact'):
factname = fact.find('name').text
if factname == target_factname: #choose the fact to pull from
for var in fact.iter(tag = 'variable'):
name = var.find('name').text
lst.append(name)
return lst #return a list of all the <name> tags from the Crash fact
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.