Вызов зевака из Python - PullRequest
1 голос
/ 21 марта 2010

Я пытаюсь таким образом вызвать gawk (реализация AWK в GNU) из Python.

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» представляет собой целое число, а не строку. Как я могу решить эту проблему?

Ответы [ 2 ]

4 голосов
/ 21 марта 2010

Это непереносимый и грязный способ проверить, есть ли что-то в файле. Представьте, что у вас 1000 линий, вы будете делать системный вызов 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
1 голос
/ 21 марта 2010

Ваша проблема в кавычках, в python что-то вроде "some test "" with quotes" не даст вам цитату. Попробуйте вместо этого:

os.system('''gawk '{if ($2=="%s") print $0}' unique_count_a_from_ac.txt''' % i)
...