我想知道在创建新file.txt
之前有没有办法找出对的值(开始,结束)。我的目标是检查我是否开始,结束值超过一对然后我想为每对创建单独的文件。如果没有,那就按原样保留。
def searchPFAM(fname):
with open(fname,'rb') as f:
root = etree.parse(f)
for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
try:
start = int(lcn.get("start"))#if it is PFAM then look for start value
end = int(lcn.get("end"))#if it is PFAM then also look for end value
yield start, end
except (TypeError , ValueError) as e:
pass
with open('newfile.txt','w') as fileinput:
for start, end in searchPFAM(fname):
print start, end
if start <= end:
text=''.join(makeList[(start-1):(end-1)])
fileinput.write(text)
答案 0 :(得分:4)
如果你想为每一对提供一个文件,你可以这样做:
for i, (start, end) in enumerate(searchPFAM(fname)):
print start, end
if start <= end:
with open('newfile-%s.txt' % i,'w') as fileinput:
text=''.join(makeList[(start-1):(end-1)])
fileinput.write(text)
希望这会有所帮助。
答案 1 :(得分:0)
据我所知,没有办法做到这一点。你可以只返回一个对象列表并获得它的长度(或者更容易,将返回值强制转换为列表):
PFAMresults=list(searchPFAM)
nresults=len(PFAMresults)
for start,end in PFAMresults:
dosomething()