。我正在努力解決下面解釋的問題。我分享不完整的Python腳本也不適用於這個問題。如果得到我的腳本的支持或指導,我將不勝感激。提取物,如果彭定康發現
文件看起來是這樣的:
<Iteration>
<Iteration_hit>Elememt1 Element1
abc1 hit 1
.
.
</Iteration>
<Iteration>
<Iteration_hit>Elememt2 Element2
abc2 hit 1
.
.
</Iteration>
<Iteration>
<Iteration_hit>Elememt3 Element3
abc3 hit 1
.
.
</Iteration>
<Iteration>
<Iteration_hit>Elememt4 Element4
abc4 hit 1
.
.
</Iteration>
我從<Iteration>
需要</Iteration>
的元素列表匹配,這意味着元素2和元素4輸出文件應該是這樣的:
<Iteration>
<Iteration_hit>Elememt2 Element2
abc2 hit 1
.
.
</Iteration>
<Iteration>
<Iteration_hit>Elememt4 Element4
abc4 hit 1
.
.
</Iteration>
腳本
#!/usr/bin/python
x = raw_input("Enter your xml file name: ")
xml = open(x)
l = raw_input("Enter your list file name: ")
lst = open(l)
Id = list()
ylist = list()
import re
for line in lst:
stuff=line.rstrip()
stuff.split()
Id.append(stuff)
for ele in Id:
for line1 in xml:
if line1.startswith(" <Iteration_hit>"):
y = line1.split()
# print y[1]
if y[1] == ele: break
你知道有ar e庫讀取/寫入xml文件,對吧? – tglaria
不要使用正則表達式解析XML。 Python爲此提供了一個'xml'包。 –
謝謝...我不知道...我會嘗試使用這些庫 – kashiff007