0

I'm trying to parse through a file and extract certain terms from that file but it keeps giving me this error and I can't figure out how to fix it so that it works. The code I have so far:

def extract(processFile, extractFile, file):
print("Extracting data from %s ..." % extractFile)

g = open(extractFile, "w+")

sentence = "";
count = 0; #Count number of lines extracted

with open(file, 'r') as f:
#for i in range(0,108084):
    for lines in iter(f):
        #Break down every line read from cacm.all
        line = lines.split()

        #If the length of a line is 0 than it reached the end of the file
        if(len(line) == 0):
            break;

        #Initialize the first word of the line being processed
        word = line[0]


        #Case 1, the line starts with .T so it must be the title
        if(word == ".T"):
            sentence = "" #the sentence will be the total title compiled into one string

            while(1): #Continue to parse through every line following .T
                line = f.readline().split()
                word = line[0]

                if((word == ".B") | (word == ".A") | (word == ".N")| (word == ".X") | (word == ".W") | (word == ".K") | (word == ".C")):
                    break;
                elif((".W" != word) & (".B" != word) & (".A" != word) & (".N" != word) & (".X" != word) & (".I" != word) & (".K" != word) & (".C" != word)):
                    sentence =  sentence + ' '.join(map(str, line)) + " "
                    continue;

            count += 1
            #Construct a line <Type of Data> <Line Count> <Sentence>
            sentence = "I " + str(count) + " " + sentence + "\n"
            g.write(sentence)

            #print(count) #Checks the total amount of titles found
            sentence = "" #Reset the sentence

It keeps giving me the error which is in the title. Any suggestions to what the problem might be?

martineau
  • 119,623
  • 25
  • 170
  • 301

0 Answers0