1

我想知道在创建新的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)
4

2 回答 2

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)

希望这会有所帮助。

于 2012-07-12T21:22:26.423 回答
0

据我所知,没有办法做到这一点。您可以只返回一个对象列表并获取它的长度(或者更简单,将返回值转换为列表):

PFAMresults=list(searchPFAM)
nresults=len(PFAMresults)
for start,end in PFAMresults:
    dosomething()
于 2012-07-12T21:21:10.163 回答