2013-04-14 37 views
0

以下代碼從txt文件中獲取每行。如果該行是「References \ n」,則該文件應該繼續獲取行,但會附加到另一個字符串中,刪除後續的\ n實例。我應該如何處理嵌套循環和休息?從文件中的循環中跳出,但仍然將文件的其餘部分作爲字符串

for file in os.listdir(txtdir): 
    if file <> '.DS_Store': 
     linenum = 1 
     refindicator = 0 
     AppendixCheck = 0 
     print 'Opening ' + str(file) + '...' 
     for line in open(txtdir + file): 
      if AppendixCheck == 0: 
       #take title from the first line 
       if linenum == 1: 
        title = line.replace(",","") 
        print "Title: " + title 
       linenum +=1 
       #checking for "references\n" line 
       if line == "References\n": 
        refindicator +=1 
       #after references are found 
       if line =='Appendix\n': 
        AppendixCheck +=1 
       if refindicator >0: 
        reflist += getline().replace('\n','') 
       #reflist = line.split(',') 
     print reflist 
+2

你的代碼示例是無效的Python。 '對於行:'是無效的,你的縮進是不正確的。 –

+0

在Python中處理文件時,使用[with'語句](http://www.youtube.com/watch?v=lRaKmobSXF4)是一種很好的做法。 –

+0

它很難理解你的代碼的邏輯與錯誤的縮進。請糾正它,以便我們提供幫助。 – scottydelta

回答

0

這裏是我找到了解決辦法:

for file in os.listdir(txtdir): 
    if file <> '.DS_Store': 
     linenum = 1 
     refindicator = 0 
     AppendixCheck = 0 
     refdump = '' 
     title = '' 
     print 'Opening ' + str(file) + '...' 
     for line in open(txtdir + file): 
      #The first blank line marks the end of the title 
      if line == '\n': 
       linenum +=1 
      #Only works until appendix is breached 
      if AppendixCheck == 0: 
       #take title from the first line 
       if linenum == 1: 
        title += line.replace('\n','').replace(',','').replace(' ', '') 
        print "Title: " + title 
       #checking for "references\n" line 
       if line == "References\n": 
        refindicator +=1 
       #after references are found 
       if 'Appendix' in line: 
        AppendixCheck +=1 
       if refindicator >0: 
        refdump += line.replace('\n','') 
     reflist = refdump.split(',') 
     print reflist 
相關問題