我正在尝试以这种方式从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“表示的值是一个整数,而不是一个字符串。我如何解决这个问题?
发布于 2010-03-21 08:47:51
你的问题在于引用,在python中,像"some test "" with quotes"
这样的东西不会给你引用。试着这样做:
os.system('''gawk '{if ($2=="%s") print $0}' unique_count_a_from_ac.txt''' % i)
发布于 2010-03-21 15:41:37
这是一种检查文件中是否有内容的不可移植且混乱的方法。假设您有1000行代码,您将对系统调用gawk 1000次。它的效率非常低。您正在使用Python,所以在Python中使用它们。
....
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( str.strip, ligand_lines )
ligand_file.close()
for line in open("unique_count_a_from_ac.txt"):
sline=line.strip().split()
if sline[1] in ligand_lines:
print line.rstrip()
或者,如果Python不是必须的,您也可以使用这一行。
gawk 'FNR==NR{a[$0]; next}($2 in a)' 2WTKA_ab.txt unique_count_a_from_ac.txt
https://stackoverflow.com/questions/2485362
复制相似问题