Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python类调用命令行_Python_Subprocess - Fatal编程技术网

从python类调用命令行

从python类调用命令行,python,subprocess,Python,Subprocess,我有一个类,它接受一个文件,执行一些进程,并输出一个以制表符分隔的文件。 我需要对这个输出文件进行排序,最简单的方法是命令行:sort-k1,1-k2,2n-k3,3n 但是,我想将其实现到python类中 我试过: from subprocess import call # classA take input file, do something, output a file, named 'tmp_output' class ClassA(object): def __init__

我有一个类,它接受一个文件,执行一些进程,并输出一个以制表符分隔的文件。 我需要对这个输出文件进行排序,最简单的方法是命令行:sort-k1,1-k2,2n-k3,3n

但是,我想将其实现到python类中

我试过:

from subprocess import call

# classA take input file, do something, output a file, named 'tmp_output'
class ClassA(object):
    def __init__(self,tmp_input,tmp_output):
        self.tmp_input = tmp_input
        self.tmp_output = tmp_output
        self.dosomething(tmp_input)

    def dosomething(self,tmp_input):
        outputfile = open(self.tmp_output,'w')
        #dosomething
        # something = ...
        outputfile.write(something)

# call this class to do something
a=ClassA('tmp_input','tmp_output') # this output a file called tmp_output

# Another class to sort tmp_output
class Sort(object):
    def __init__(self,tmp_output,output):
        self.tmp_output = tmp_output
        self.output = output

    def sort(self):
        call('sort -k1,1 -k2,2n -k3,3n self.tmp_output > self.output') # THIS STEP DOES NOT WORK

我的问题是如何调用sort-k1,1-k2,2n-k3,3n,因为输入和输出来自类变量。

为什么?排序有什么问题?我想先用第一列排序,然后用数字排序,然后用数字排序。你可以通过键或cmp参数指定你想排序的任何排序条件。你可能想试试,调用'sort-k1,1-k2,2n-k3,3n{}>{}'。格式化self.tmp_输出,self.outputHanks,我正在尝试分类。学生元组=[76]中的[john',A',15',jane',B',12',dave',B',10]:分类学生元组,key=lambda x:x[0],x[1]Out[76]:[dave',B',10',jane',B',12',john',A',15]它不使用第二个键。你能告诉我怎么了吗?