我需要一些python程序的帮助。我试了很多东西,好几个小时了,但都不管用。在
有谁能帮我吗?在
这就是我需要的:
- 我有一个文件:http://www.filedropper.com,其中包含有关蛋白质的信息。在
- 我只想过滤那些与…存在相匹配的蛋白质。在
- 从这些蛋白质中,我只需要。。。(6个标记的文本,在
>sp|
之后,以及物种(第二行,在[]之间) - 我想要。。最后在一张桌子上。在
....
Human AAA111
Mouse BBB222
Fruit fly CCC333
到目前为止我所拥有的:
^{pr2}$
如果你能帮助我,你就是英雄!在
Tags:
这似乎对您的示例文本有效。我不知道每个文件是否可以有一个以上的提取,我在这里时间不够,所以如果需要,您必须扩展它:
我的第一个建议是在打开文件时使用
with
语句:您的} 运算符在字符串
FilterOnRegEx
方法的问题是:if ploop in line
。带字符串参数的^{line
中搜索ploop
中的确切文本。在相反,您需要compile将文本形式转换为re对象,然后search来匹配:
^{pr2}$这将有助于你向前迈进。在
下一步,我建议学习generators。打印匹配的行很好,但这无助于您对它们进行进一步的操作。我可能会将
print
更改为yield
,这样我就可以进一步处理数据,例如提取所需的部分并重新格式化以供输出。在作为一个简单的演示:
附录:我使用您发布的数据样本运行了上面我发布的代码,它成功地打印了一些行而不是其他行。换句话说,正则表达式确实匹配某些行,而不匹配其他行。到现在为止,一直都还不错。但是,,您需要的数据并不是全部在输入的一行上!这意味着在模式上过滤单个行是不够的。(当然,除非我在问题中看不到正确的换行符)问题中的数据方式,您需要实现一个更健壮的解析器,该解析器具有状态,以了解记录何时开始、何时结束以及记录中间的任何给定行。在
相关问题 更多 >
编程相关推荐