1

我试图以这种方式从 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”表示的值是整数而不是字符串。我该如何解决这个问题?

4

2 回答 2

4

这是检查文件中是否有内容的一种不可移植且混乱的方式。假设您有 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
于 2010-03-21T07:41:37.160 回答
1

您的问题出在报价中,在 python 中,类似的东西"some test "" with quotes"不会给您报价。试试这个:

os.system('''gawk '{if ($2=="%s") print $0}' unique_count_a_from_ac.txt''' % i)
于 2010-03-21T00:47:50.627 回答