0

The following code gets each line from a txt file. If the line is "References\n" the file should continue to getlines, but appended to another string removing the subsequent '\n' instances. How should I deal with the nested loops and breaks?

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
raw-bin hood
  • 5,839
  • 6
  • 31
  • 45

1 Answers1

0

Here is the solution I found:

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
raw-bin hood
  • 5,839
  • 6
  • 31
  • 45