python实现txt文件内容处理(去重序列及排序,求唯一序列)
有如下生物信息学数据,存在内容完全相同的序列。
- 需求1:找出相同序列,然后在文件中只保留他们的一个副本,并在文件中分别保留对应的原始标识符列表
- 需求2:在另外一个文件保存唯一序列
需求1
#去重序列及排序
lines_seen = set()
readDir = "123.txt"
f = open(readDir,"r")
writeDir = "456.txt"
outfile=open(writeDir,"w")
while True:
line1 = f.readline()
line2 = f.readline()
if not line2: break # EOF
if line2 not in lines_seen:
outfile.write(line1)
lines_seen.add(line2)
e = open(readDir, "r")
while True:
line3 = e.readline()
line4 = e.readline()
if not line4: break # EOF
if (line4 == line2) and (line3 != line1):
outfile.write(line3)
outfile.write(line2)
outfile.close()
print ("success")
result:
需求2
#求唯一序列
readDir = "123.txt"
writeDir = "789.txt"
lines_seen = set()
lines_remove = set()
seen = set()
outfile=open(writeDir,"w")
f = open(readDir,"r")
while True:
line1 = f.readline()
line2 = f.readline()
if not line2: break # EOF
if line2 in seen:
lines_remove.add(line2)
else:
seen.add(line2)
e = open(readDir,"r")
while True:
line1 = e.readline()
line2 = e.readline()
if not line2: break # EOF
if line2 not in lines_remove:
outfile.write(line1)
outfile.write(line2)
outfile.close()
print ("success")
result: