0

我正在尝试从 python 调用 unix 排序命令来对制表符分隔的文件进行排序,但我不能。当我用谷歌搜索它时,我发现在命令中添加 -t$'\t' 是有效的,当我尝试从 shell 排序时我也为我工作,但当我尝试从 python 时它不起作用。

这是我在脚本中尝试的

tabdel="$'\t'"
sort_file_cmd="sort -t {1} -k2,2 -k6,6n {0}".format(file_to_be_sorted.name,tabdel)
print sort_file_cmd,shlex.split(sort_file_cmd)
subprocess.call(sort_file_cmd,stdout=sort_bt,shell=True)

print sort_file_cmd打印这个

sort -t $'\t' -k2,2 -k6,6n human_vs_mouse.tab

从 shell 运行时效果很好,但从 python 脚本运行时会产生排序:多字符选项卡'$\t'错误

我该如何克服呢?

样本数据

gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|10980 gi|58801268|ref|NP_001011737.1| olfactory receptor 1357 [Mus musculus]  3071921 1   307 1.90237e-150    1108.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|460 gi|22129025|ref|NP_667153.1| olfactory receptor 351 [Mus musculus]    302 10  915 2   303 5.70073e-105    806.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|4490 gi|33238878|ref|NP_666817.1| olfactory receptor 24 [Mus musculus]    308 1   921 1   307 9.58658e-105    805.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|458 gi|22129031|ref|NP_667152.1| olfactory receptor 353 [Mus musculus]    302 10  915 2   303 1.01585e-103    798.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|13639 gi|268837230|ref|NP_667200.2| olfactory receptor 1496 [Mus musculus]    3071921 3   309 1.50986e-99 771.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|13345 gi|283837936|ref|NP_666450.2| olfactory receptor 374 [Mus musculus] 310 1   930 1   310 4.18033e-99 768.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|455 gi|22129035|ref|NP_667150.1| olfactory receptor 354 [Mus musculus]    302 13  918 8   309 1.85488e-98 764.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|410 gi|22129071|ref|NP_667122.1| olfactory receptor 1377 [Mus musculus]   305 1   915 1   304 3.06622e-97 755.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|312 gi|53933206|ref|NP_001005569.1| olfactory receptor 366 [Mus musculus] 307 1   921 1   307 2.14345e-96 749.0  
gi|52317161|ref|NM_001004713.1| Homo sapiens olfactory receptor, family 1, subfamily I, member 1 (OR1I1), mRNA  gnl|BL_ORD_ID|4458 gi|58801284|ref|NP_001011748.1| olfactory receptor 867 [Mus musculus]    3091927 1   309 7.36974e-96 748.0   
4

2 回答 2

1

您可以避免使用shell=True,只需使用 alist而不是字符串作为命令:

>>> subprocess.call(['sort', '-t', '\t', 'testing.txt'])
a       b
c       d
0

不是构建单个流,而是使用字符串列表,每个字符串代表原始命令中的一个标记。在您的情况下,完整的“命令行”将是:

subprocess.call(['sort', '-t', '\t', '-k2,2', '-k6,6n', file_to_be_sorted.name])

请注意,这种方法更安全,您应该尽量避免shell=True。仅当您想使用某些shell功能时才必须使用,例如内置命令、循环、管道(尽管这些可以构建使用...)等。在您只想执行传递一些参数的命令的所有情况下,可以避免。shell=Trueifsubprocess.Popenshell=True

您可以使用shlex.split为了从命令字符串中获取字符串列表:

>>> cmdline = shlex.split("sort -t '\t' -k2,2 -k6,6n")
>>> cmdline
['sort', '-t', '\t', '-k2,2', '-k6,6n']
>>> cmdline.append(file_to_be_sorted.name)   # insert the last argument.

请注意,在这种情况下,您必须\t将原始字符串中的单引号括起来。


如果您仍然想使用,shell=True那么只需不要使用转义$'\t'

>>> subprocess.call("sort -t '\t' testing.txt", shell=True)
a       b
c       d
0

对我来说很好。

于 2013-11-08T12:15:31.907 回答
0

$' '引用语法由 shell 解释。

你需要说:

tabdel=r'\t'
于 2013-11-08T12:19:28.047 回答