我正在尝试解析文件并从该文件中提取某些术语,但它一直给我这个错误,我无法弄清楚如何修复它以便它可以工作。我到目前为止的代码:
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
它一直给我标题中的错误。对问题可能有什么建议吗?