Python 使用re解析.xml blast输出

Python 使用re解析.xml blast输出,python,xml,parsing,blast,Python,Xml,Parsing,Blast,我正在尝试使用re解析XML格式的BLAST输出,以前从未这样做过,下面是我的代码 然而,由于一些点击次数有Hsp\u num有时不止一次,我从和query\u到得到的query\u结果更多,而query\u len的结果更少,如何指定如果Hsp\u num大于1,则再次打印查询len?多谢各位 import re output = open('result.txt','w') n = 0 with open('file.xml','r') as xml: for line in xml

我正在尝试使用
re
解析XML格式的BLAST输出,以前从未这样做过,下面是我的代码

然而,由于一些点击次数有
Hsp\u num
有时不止一次,我从
query\u到
得到的
query\u结果更多,而
query\u len
的结果更少,如何指定如果
Hsp\u num
大于1,则再次打印查询len
?多谢各位

import re
output = open('result.txt','w')
n = 0
with open('file.xml','r') as xml:
    for line in xml:
         if re.search('<Hsp_query-from>', line) != None:
             line = line.strip()
             line = line.rstrip()
             line = line.strip('<Hsp_query-from>')
             line = line.rstrip('</')
             query_from = line
         if re.search('<Hsp_query-to>', line) != None:
             line = line.strip()
             line = line.rstrip()
             line = line.strip('<Hsp_query-to>')
             line = line.rstrip('</')
             query_to = line
         if re.search('<Hsp_num>', line) != None:
             line = line.strip()
             line = line.rstrip()
             line = line.strip('<Hsp_num>')
             line = line.rstrip('</')
             Hsp_num = line
             print >> output, Hsp_num+'\t'+query_from+'\t'+query_to
output.close()

你熟悉吗?它的模块可能正是您所需要的。本教程和菜谱的最后一部分都是关于BLAST的,第7.3节讨论解析。您将了解它是如何工作的,这将比使用正则表达式解析XML容易得多,这只会导致流泪和流泪。

谢谢,我看了那一章,似乎很复杂,无论如何,我想我将放弃解析.XML格式并使用表格格式。我只是想知道这样的事情在re身上是否可行
with open('file.xml','r') as xml:
    for line in xml:
        if re.search('<Iteration_query-len>', line) != None:
            line = line.strip()
            line = line.rstrip()
            line = line.strip('<Iteration_query-len>')
            line = line.rstrip('</')
            query_len = line