2014-08-29 16 views
0

我有這個文件的問題。當我啓動它時,我收到第24行的錯誤。UnboundLocal錯誤文件

UnboundLocal Error : local variable 'file_out' referenced before assignment. 

有關如何更正它的任何建議將不勝感激。我不得不說,我是一個完整的Python的noob,並沒有自己寫這個。

#!/usr/local/python 

import sys, getopt 
import os 

usage="python correct_mol2.py -i 2qab_ligand.mol2 -o 2qab_ligand_new.mol2\n" 

def main(argv): 

    try: 
     opts,args = getopt.getopt(sys.argv[1:],'hi:o:') 
    except getopt.GetoptError: 
     sys.exit(2) 

    for opt, arg in opts: 
     if opt == '-h': 
      print usage 
      sys.exit() 
     elif opt == '-i': 
       file_in = arg 
     elif opt == '-o': 
      file_out = arg 
    x=0 
    fout=file("%s"%file_out,"w") 
    for line in file(file_in): 
#  print line 
     if line.find("@<TRIPOS>BOND") >= 0: 
      x=0 
     if x==0: 
      fout.write(line) 
     if x==1: 
      if line[47:49] == '35' : 
       fout.write(line[:47]+"Br"+line[49:]) 
       continue 
      if line[47:49] == '17' : 
       fout.write(line[:47]+"Cl"+line[49:]) 
       continue 
      if line[47:48] == '9' : 
       fout.write(line[:47]+"F"+line[48:]) 
       continue 
      if (line[47] == 'H' and line[48] ==' ') or line[47] == 'F' or line[47:49] == 'Br' or line[47:49] == 'Cl' : 
       fout.write(line) 
       continue 
      else: 
       fout.write(line[:48]+"."+line[48:54]+line[55:]) 
     if line.find("@<TRIPOS>ATOM") >= 0: 
      x=1 
    fout.close() 

main(sys.argv) 

回答

1

file_out說法是只有集如果-o參數設置。

您需要將for opt, arg in opts:循環之前可能設置默認:

file_out = 'default_filename' 

如果-o意味着是一個強制性的選擇,你需要明確地測試選擇缺席。

您的代碼的其他意見:

  • 使用argparse代替; optparse已被棄用,它的繼任者更加靈活多變。

  • file_out在設置時已經是一個字符串。不需要使用字符串格式;您可以直接將其傳遞給open()(而不是file(),也不贊成使用)。如果您使用的文件對象爲context manager,然後它會自動關閉你:

    with open(file_out, "w") as fout, open(file_in) as fin: 
        for line in fin: 
    
  • 您可以使用文件對象作爲一個迭代器,這意味着你可以提前文件對象,並獲得更多的線隨着解析狀態的變化,for循環。用它來檢測Tripos MOL2記錄。

    Tripos MOL2 data records使用製表符或空格分隔行;將你的行分割成列,然後挑出特定的列來映射替換值。這比切片行到特定的列少了很多脆弱:

    map = {'35': 'Br', '17': 'Cl', '9': 'F'} 
    # when we encounter a mapped value, make it easier on ourselves 
    # and map those back to themselves 
    map.update((v: v) for v in map.values()) 
    
    section = None 
    for line in fin: 
        if line[0] == '@': 
         # new section 
         section = line.strip().rpartition('>')[-1] 
         fout.write(line) 
         continue 
    
        if section != 'ATOM': 
         fout.write(line) 
         continue 
    
        # parse the ATOM section 
        for line in fin: 
         if line[0] == '@': # section end, we are done 
          section = line.strip().rpartition('>')[-1] 
          fout.write(line) 
          break 
    
         # atom data lines are whitespace separated 
         row = line.split() 
    
         # I'm assuming column 7 is the value you are replacing; adjust as required 
         row[7] = map.get(row[7], '.') 
    
         # re-join line with tabs, write out 
         fout.write('\t'.join(row) + '\n') 
    
+0

非常感謝您爲您詳細的答覆。 主要問題是我真的不懂編程。我真的不知道要替換什麼以及如何替換。 我很慚愧地問你,但你能否重寫腳本,所以我只需要複製它並使其工作? 我是一名藥物化學科學家,因此它將幫助我的研究專注於癌症。 – 2014-09-01 07:34:58

+0

@XavierDrozak:我只做了一個有根據的猜測,可能會做些什麼來改進您的代碼。我無法完全爲您重新編寫您的代碼;我無法訪問您擁有的數據,也無法爲您測試。找一個能編程的人,或者可以教你在附近進行編程,在一個更適合指導的環境中進行編程。我可以通過在線輔導計劃獲得(請參閱我的個人資料)。祝你好運! – 2014-09-01 07:47:31