我的outputfile中的数据没有打印它应该具有的所有结果

时间:2012-07-12 19:42:26

标签: python list return lxml

我正在尝试将makeList [start]的索引输出到makeList [end]。我有所有正确的开始和结束值,总共约11对。但是,我没有得到正确的输出,因为它没有打印出所有可能的输出。为什么会这样?

def searchPFAM(fname):
   with open(fname,'rb') as f:
       root = etree.parse(f)
       for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):
          try:
             start = int(lcn.get("start"))
             end = int(lcn.get("end"))
             yield start, end
          except (TypeError , ValueError) as e:
             pass 

uniprotID ='Q14591'

def createRef():
    fileInput = open(uniprotID +'.txt','r')
    readAA = fileInput.read()
    store_value = (readAA.partition('\n'))
    aaSequence = store_value[2].replace('\n', '')
    makeList = list(aaSequence)
    return makeList

makeList = createRef()

for start, end in searchPFAM(fname):
    print start, end  
    if start <= end:
        with open('newfile.txt','w') as fileinput:
            while start <= end:
                result = makeList[start]
                fileinput.write(result)
                start += 1

2 个答案:

答案 0 :(得分:3)

只是一个猜测,但你可能想要:

with open('newfile.txt','w') as fileinput:
    for start, end in searchPFAM(fname):
        print start, end  
        if start <= end:
            while start <= end:
                result = makeList[start]
                fileinput.write(result)
                start += 1

您的旧版本只将1个开始/结束循环写入文件,关闭它然后打开一个新文件(与旧文件同名),写入1个开始/结束循环,关闭文件...在其他单词,你在每次循环迭代时重写文件。

答案 1 :(得分:0)

最常见的原因:您需要在脚本终止之前刷新输出文件。

上面的

片段:

with open('newfile.txt','w') as fileinput:
        while start <= end:
            result = makeList[start]
            fileinput.write(result)
            start += 1
fileinput.flush()