簡體   English   中英

無法使用python解析XML文件。想從python文件中刪除一行。請幫助我刪除該行

[英]Unable to parse XML file with python.Want to remove a line from python file for it .Help me to remove the line

您好我正在使用etree解析xml文件。解析xml文件時遇到問題。以下是詳細信息。

<niktoscan .................................... >#don't want to remove this line
<scandetails>
data 
</scandetials>
<niktoscan ....................................> #line 1 to remove
<scandetails>
data
</scandetials>
 <niktoscan ....................................> #line 2 to remove
<scandetails>
data
</scandetials>
</niktoscan>

如您在上面的代碼中看到的,niktoscan再次出現而沒有關閉標簽。我想要的是刪除開始和結束之間的niktoscans行,僅留下第一個niktoscan標簽。 我很困惑如何刪除niktoscan行。 使用python幫助我解決這個問題。

您可以使用它來解析文件:

with open('niktoscan.txt') as f:
    content = f.readlines()

foundone = False
print type(content)

cleanedContent = []
for line in content:
    print line

    foundnik = line.find('<niktoscan')
    if not (foundnik != -1 and foundone):
        cleanedContent.append(line)

    if foundnik != -1:
        foundone = True
print "\n\n ########### cleaned content ########### \n\n"

for line in cleanedContent:
    print line

然后,您可以將結果放入解析器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM