从 Python 调用 gawk
我正在尝试从 Python 调用 gawk (AWK 的 GNU 实现)以这种方式。
import os
import string
import codecs
ligand_file=open( "2WTKA_ab.txt", "r" ) #Open the receptor.txt file
ligand_lines=ligand_file.readlines() # Read all the lines into the array
ligand_lines=map( string.strip, ligand_lines )
ligand_file.close()
for i in ligand_lines:
os.system ( " gawk %s %s"%( "'{if ($2==""i"") print $0}'", 'unique_count_a_from_ac.txt' ) )
我的问题是“i”没有被它代表的值替换。值“i”代表的是整数而不是字符串。我该如何解决这个问题?
I am trying to call gawk (the GNU implementation of AWK) from Python in this manner.
import os
import string
import codecs
ligand_file=open( "2WTKA_ab.txt", "r" ) #Open the receptor.txt file
ligand_lines=ligand_file.readlines() # Read all the lines into the array
ligand_lines=map( string.strip, ligand_lines )
ligand_file.close()
for i in ligand_lines:
os.system ( " gawk %s %s"%( "'{if ($2==""i"") print $0}'", 'unique_count_a_from_ac.txt' ) )
My problem is that "i" is not being replaced by the value it represent. The value "i" represents is an integer and not a string. How can I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种不可移植且混乱的检查文件中是否有内容的方法。想象一下你有 1000 行,你将对 gawk 进行 1000 次系统调用。效率超级低啊你正在使用Python,所以也用Python来做它们。
或者,如果 Python 不是必须的,您也可以使用这一行。
That's a non-portable and messy way to check if something is in a file. Imagine you have 1000 lines, you will be making system call to gawk 1000 times. It's super inefficient. You are using Python, so do them in Python.
Or you can also use this one liner if Python is not a must.
你的问题出在引用上,在Python中,像
"some test "" withquotes"
这样的东西不会给你报价。试试这个:Your problem is in the quoting, in python something like
"some test "" with quotes"
will not give you a quote. Try this instead: