我有1000个.txt
文件,我在这些文件上运行这段代码。我要做的是在文件中找到ENSG的最大值,并删除小于最大值的其他ENSG值。然后我必须通过查看utr长度从同一个文本文件中找到最小值,并将其保存在我的output.txt
文件中。我想要在1output.txt
文件中输出1000.txt
文件。这段代码工作得很好,但是它覆盖了结果。它只显示最后一个文件的结果:
import glob
f2 = glob.glob("./*.txt")
all_text=""
for fpath in f2:
f = open(fpath,"r")
list_lines = f.readlines()
dic={}
sent="ENSG"
temp_list=[]
for line in list_lines:
all_text=all_text+line
name= line.rsplit()[0].strip()
score=line.rsplit()[1].strip()
dic[name]=score
for i in dic.keys():
if sent in i:
temp_list.append(dic[i])
hiegh_score=max(temp_list)
def check(index):
reverse_text=all_text[index+1::-1]
index2=reverse_text.find("\n")
if sent==reverse_text[:index2+1][::-1][1:len(sent)+1]:
return False
else:
return True
list_to_min=dic.values()
for i in temp_list:
if i!=hiegh_score:
index=all_text.find(str(i))
while check(index):
index=all_text.find(str(i),index+len(str(i)))
all_text=all_text[0:index]+all_text[index+len(str(i)):]
list_to_min.remove(str(i))
file2=open("my_try4.txt","w")
file2.write(all_text)
min_score= min(list_to_min)
for j in dic.keys():
if min_score==dic[j]:
k="min score is :"+str(min_score)+" for person "+j
file2.write(k)
print "%6d : %s" % (len(list_lines),fpath)
file2.close()
f.close()
我有这样的文本文件
4.txt
:
现在它应该选择ENSG和1935,并删除ENSG的所有其他值。现在文本文件应该如下所示:
ENSBTAG00000020679 197
ENSCAFG00000009872 2585
ENSG00000018236 1935
ENSRNOG00000004438 14
ENSRNOG00000004438 14
现在,通过查看这个文本文件,我们找到最短的值并将其保存在一个文本文件中(我们在1000个文件上执行此操作,并且输出应该在1个文件中)。在
output.txt
textfile4 14
重写它比找出代码的错误要容易得多:
入口点是
main()
,它被称为main('txtdir', 'ENSG', 'output.txt')
。对于每个文件,filter_max_returning_min()
将打开并重写该文件并返回最小值。没有必要保留你访问过的每一行文件的dict或list。在(顺便说一句,破坏性地重写文件似乎是个坏主意!你考虑过把它们复制到别处吗?)在
当您将不同的关注点隔离到单独的函数中时,很容易针对不同的执行行为重新组合它们。例如,通过添加两个小函数,在所有文件上并行运行此任务很简单:
^{pr2}$现在您可以启动一个可配置数量的worker,每个worker将在一个文件上工作,并在完成后收集它们的最小值。如果您有非常大的文件或大量的文件,并且没有IO绑定,这可能会更快。在
为了好玩,您还可以轻松地将其转换为CLI实用程序:
现在可以从命令行调用它:
相关问题 更多 >
编程相关推荐