python 中的 awk:如何在 python 类中使用 awk 脚本?
我正在尝试使用 python 运行 awk 脚本,这样我就可以处理一些数据。
有没有办法让 awk 脚本在 python 类中运行,而不使用系统类将其作为 shell 进程调用?我运行这些 python 脚本的框架不允许使用子进程调用,所以我要么想办法在 python 中转换我的 awk 脚本,要么如果可能的话,在 python 中运行 awk 脚本。
有什么建议吗?我的 awk 脚本基本上读取一个文本文件并分离包含特定化合物的蛋白质块(输出由我们的框架生成;我添加了一个示例,如下所示)并将它们分离出来并打印在不同的文件。
buildProtein compoundA compoundB
begin fusion
Calculate : (lots of text here on multiple lines)
(more lines)
Final result - H20: value CO2: value Compound: value
Other Compounds X: Value Y: value Z:value
[...another similar block]
例如,如果我构建一个蛋白质,我需要查看最终结果行中是否有 CH3COOH 的化合物,如果有,我必须从命令“buildProtein”开始,直到结果行的开头下一个区块;并将其保存在文件中;然后移至下一个,看看它是否再次具有我正在寻找的化合物...如果没有,我跳到下一个,直到文件末尾(该文件多次出现该化合物我搜索,有时它们是连续的,而有时它们与没有复合的块交替使用,
任何帮助都是非常受欢迎的;在发现这个网站后,我决定寻求一些帮助,
谢谢 。提前感谢您的好意!
I am trying to run an awk script using python, so I can process some data.
Is there any way to get an awk script to run in a python class without using the system class to invoke it as shell process? The framework where I run these python scripts does not allow the use of a subprocess call, so I am stuck either figuring out a way to convert my awk script in python, or if is possible, running the awk script in python.
Any suggestions? My awk script basically read a text file and isolate blocks of proteins that contains a specific chemical compound (the output is generated by our framework; I've add an example of how does it looks like below) and isolate them printing them out on a different file.
buildProtein compoundA compoundB
begin fusion
Calculate : (lots of text here on multiple lines)
(more lines)
Final result - H20: value CO2: value Compound: value
Other Compounds X: Value Y: value Z:value
[...another similar block]
So for example if I build a protein and I need to see if in the compounds I have CH3COOH in the final result line, if it does I have to take the whole block, starting from the command "buildProtein", until the beginning of the next block; and save it on a file; and then move to the next and see if it has again the compound that I am looking for...if it does not have it I skip to the next, until the end of the file (the file has multiple occurrence of the compound that I search for, sometimes they are contiguous while other times they are alternate with blocks that has not the compound.
Any help is more than welcome; banging my head for weeks now and after finding out this site I decided to ask for some help.
Thanks in advance for your kindness!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您无法使用 subprocess 模块,最好的办法是用 Python 重新编码 AWK 脚本。为此,fileinput 模块是一个很棒的转换工具,具有类似 AWK 的感觉。
If you can't use the subprocess module, the best bet is to recode your AWK script in Python. To that end, the fileinput module is a great transition tool with and AWK-like feel.
Python 的 re 模块 可以提供帮助,或者,如果您不介意正则表达式和只需要进行一些快速的字段分隔,您可以使用 内置的 str
.split()
和.find()
函数。Python's re module can help, or, if you can't be bothered with regular expressions and just need to do some quick field seperation, you can use the built in str
.split()
and.find()
functions.我刚刚开始学习 AWK,所以我无法在这方面提供任何建议。然而,对于一些满足您需要的 Python 代码:
我们这里有一个 build Protein 文本文件的迭代器,它一次返回一个蛋白质作为
Protein
对象。这个 Protein 对象足够聪明,可以知道它的输入、最终结果和其他结果。如果文件中的实际文本与问题中所表示的不完全一样,您可能需要修改一些代码。以下是对代码的简短测试和示例用法:我没有费心保存值,但如果您愿意,可以添加它。希望这能让你继续前进。
I have barely started learning AWK, so I can't offer any advice on that front. However, for some python code that does what you need:
What we have here is an iterator for the buildprotein text file which returns one protein at a time as a
Protein
object. ThisProtein
object is smart enough to know it's inputs, final results, and other results. You may have to modify some of the code if the actual text in the file is not exactly as represented in the question. Following is a short test of the code with example usage:I didn't bother saving values, but you can add that in if you like. Hopefully this will get you going.