排序制表符分隔文件

时间:2013-11-08 12:10:22

标签: python shell sorting

我试图从python调用unix sort命令来对制表符分隔文件进行排序,但我无法解决。当我搜索它时,我发现在命令中添加-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脚本运行时产生sort:多字符选项卡'$ \ 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   

2 个答案:

答案 0 :(得分:1)

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

>>> 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=True,例如内置命令if,循环,管道(尽管这些可以构建使用subprocess.Popen ...)等。在您只想执行传递某些参数的命令的所有情况下,都可以避免使用shell=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

对我来说很好。

答案 1 :(得分:0)

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

你需要说:

tabdel=r'\t'