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が必須でない場合は、この1つのライナーを使用することもできます。

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 に答える