Sorting 写入文件时出错
我编写了一个python脚本,它使用子流程模块调用unix排序。我试图根据两列(2和6)对表进行排序。这就是我所做的Sorting 写入文件时出错,sorting,python-2.7,subprocess,Sorting,Python 2.7,Subprocess,我编写了一个python脚本,它使用子流程模块调用unix排序。我试图根据两列(2和6)对表进行排序。这就是我所做的 sort_bt=open("sort_blast.txt",'w+') sort_file_cmd="sort -k2,2 -k6,6n {0}".format(tab.name) subprocess.call(sort_file_cmd,stdout=sort_bt,shell=True) 但是,输出文件包含一个不完整的行,当我解析表时会产生错误,但当我检查输入文件中的条目
sort_bt=open("sort_blast.txt",'w+')
sort_file_cmd="sort -k2,2 -k6,6n {0}".format(tab.name)
subprocess.call(sort_file_cmd,stdout=sort_bt,shell=True)
但是,输出文件包含一个不完整的行,当我解析表时会产生错误,但当我检查输入文件中的条目以对该行进行排序时,该行看起来很完美。我想当sort试图将结果写入指定的文件时会出现一些问题,但我不确定如何解决这个问题
该行在输入文件中如下所示
gi | 191252805 | ref | NM | U 001128633.1 |智人轮辋结合蛋白3C(RIMBP3C),mRNA gnl | BL | U ORD | ID 12414GI | 124487059 | ref | NP U 001074857.1 |轮辋结合蛋白2[小家鼠]103 2877 3176 846 941.0102e-07 138.0
但在输出文件中,仅打印gi | 19125。我如何解决这个问题
任何帮助都将不胜感激
Ram考虑到python有一个用于排序项目的内置方法,使用subprocess调用外部排序工具似乎非常愚蠢 查看示例数据,它似乎是结构化数据,带有
分隔符。以下是如何打开该文件,并以排序方式在python中迭代结果:
def custom_sorter(first, second):
""" A Custom Sort function which compares items
based on the value in the 2nd and 6th columns. """
# First, we break the line into a list
first_items, second_items = first.split(u'|'), second.split(u'|') # Split on the pipe character.
if len(first_items) >= 6 and len(second_items) >= 6:
# We have enough items to compare
if (first_items[1], first_items[5]) > (second_items[1], second_items[5]):
return 1
elif (first_items[1], first_items[5]) < (second_items[1], second_items[5]):
return -1
else: # They are the same
return 0 # Order doesn't matter then
else:
return 0
with open(src_file_path, 'r') as src_file:
data = src_file.read() # Read in the src file all at once. Hope the file isn't too big!
with open(dst_sorted_file_path, 'w+') as dst_sorted_file:
for line in sorted(data.splitlines(), cmp = custom_sorter): # Sort the data on the fly
dst_sorted_file.write(line) # Write the line to the dst_file.
def自定义分拣机(第一、第二):
“”“用于比较项目的自定义排序函数。”
基于第2列和第6列中的值。”“”
#首先,我们把这一行分成一个列表
first_items,second_items=first.split(u'|'),second.split(u'|')#在管道字符上拆分。
如果len(第一项)>=6,len(第二项)>=6:
#我们有足够的项目来比较
如果(第一项[1],第一项[5])>(第二项[1],第二项[5]):
返回1
elif(第一项[1],第一项[5])<(第二项[1],第二项[5]):
返回-1
否则:#它们是一样的
返回0#那么订单就无关紧要了
其他:
返回0
打开(src_文件路径,'r')作为src_文件:
data=src_file.read()#一次性读取src文件。希望文件不要太大!
打开(dst_排序的_文件路径“w+”)作为dst_排序的_文件:
对于已排序的行(data.splitlines(),cmp=custom_sorter):#动态排序数据
dst_排序的_文件。写入(行)#将行写入dst_文件。
仅供参考,此代码可能需要一些抖动。我测试得不太好。您看到的可能是试图同时从多个进程写入文件的结果 要模拟:
sort-k2,2-k6,6n${tabname}>sort_blast.txt
Python中的命令:
from subprocess import check_call
with open("sort_blast.txt",'wb') as output_file:
check_call("sort -k2,2 -k6,6n".split() + [tab.name], stdout=output_file)
您可以用纯Python编写它,例如,对于一个小的输入文件:
def custom_key(line):
fields = line.split() # split line on any whitespace
return fields[1], float(fields[5]) # Python uses zero-based indexing
with open(tab.name) as input_file, open("sort_blast.txt", 'w') as output_file:
L = input_file.read().splitlines() # read from the input file
L.sort(key=custom_key) # sort it
output_file.write("\n".join(L)) # write to the output file
如果需要对不适合内存的文件进行排序;请参见无需使用
cmp
参数。每行只处理一次<代码>cmp在Python 3中被完全删除。