2012-07-24 97 views
1

我正在嘗試編寫代碼,以便消除結果文件中的重複項,即「N/A」行和下面的行。這裏是我有:嘗試和錯誤,除了突破?

with open('false_'+uniprotID+'.txt','w') as fileinput:   
     for index, (start, end) in enumerate(searchPFAM(fname)):  
      for item in lookup[uniprotID]: 
       for names in wholelookup[uniprotID]: 
        if re.search(r'\d+',names).group(0)==item and start <= int(item) <= end: 
         result = str(int(item) - start + 1) 
         try: 
          fileinput.write(">{0} | at position {1} | start= {2}, end= {3} | description: {4}\n".format(uniprotID, result, start, end, names)) 
          fileinput.write(''.join(makeList[start-1:end])) 
          textwrap.wrap(''.join(makeList[start-1:end]),width = 60) 
          fileinput.write('\n') 
         except ErrorIO as e: 
          break 
        else: 
         fileinput.write(">{0} | N/A | start= {1}, end= {2} | description: {3} \n".format(uniprotID, start, end, names)) 
         fileinput.write(''.join(makeList[start-1:end])) 
         textwrap.wrap(''.join(makeList[start-1:end]),width = 60) 
         fileinput.write('\n') 

我的結果文件是這樣的:

Q14591 |在位置4 |開始= 174,結束= 196 |說明:A177T

YQCRHCSKSFSQRSDLVKHQRIH

Q14591 | N/A |開始= 174,結束= 196 |說明:M418T

YQCRHCSKSFSQRSDLVKHQRIH

Q14591 |在位置21 |開始= 398,結束= 420 |描述:M418T YACSDCTKSFSRRSDLVKHQRIH

Q14591 | N/A |開始= 398,結束= 420 |說明:M418T

YACSDCTKSFSRRSDLVKHQRIH

+0

您是否期待'try:'塊中的代碼引發任何特定的錯誤?如果沒有,'嘗試/除外'可能不是處理你重複的方式。 – Marius 2012-07-24 00:44:15

+0

@Marius我應該怎樣處理這個問題?你有什麼建議嗎?在此先感謝:) – 2012-07-24 00:45:32

+0

我一點都不確定是什麼讓你對這段代碼感到不滿...... – 2012-07-24 00:47:55

回答

1

你爲什麼不過濾掉它們呢?

0

照顧你有重複的四行代碼,並從他們創造的功能。然後從兩個地方調用該函數。您必須對差異進行參數化,也就是說,爲兩個調用之間的差異提供不同的值可以給出一個參數。

例如:

def do_the_common_thing(fileinput, uniprotID, result, start, end, names): 
    fileinput.write(">{0} | {1} | start= {2}, end= {3} | description: {4}\n".format(uniprotID, result, start, end, names)) 
    fileinput.write(''.join(makeList[start-1:end])) 
    textwrap.wrap(''.join(makeList[start-1:end]),width = 60) 
    fileinput.write('\n') 

這是一個很大的爭論,你也許可以想出一個更好的重構。

+0

這樣做不會有任何區別,但..是嗎?我不確定 – 2012-07-24 00:53:59

+0

對不起,我以爲你試圖刪除的「重複」是重複的代碼。這對你的問題沒有幫助,但無論如何這是個好主意...... :) – 2012-07-24 00:56:29

+0

不,我的意思是結果文件中的重複 – 2012-07-24 00:56:52