简体   繁体   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

hello I am using etree for the parsing the xml file.I am having a problem parsing a xml file.Below are details. 您好我正在使用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>

As you can see in above code niktoscan is coming again without closing tag.What i want is to remove the niktoscans lines between the start and end leaving only first niktoscan tag. 如您在上面的代码中看到的,niktoscan再次出现而没有关闭标签。我想要的是删除开始和结束之间的niktoscans行,仅留下第一个niktoscan标签。 I am confuse how to remove the niktoscan lines. 我很困惑如何删除niktoscan行。 Help me on this ploblem with python. 使用python帮助我解决这个问题。

You can use this to parse your file: 您可以使用它来解析文件:

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

Then you can put the results through your parser. 然后,您可以将结果放入解析器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM