我正在使用python做作业。我的想法是,我需要提取包含动词“给”和介词“到”的所有句子(例如,我将花束给予女孩)。我想出了一个代码,向我报告错误的缩进空间错误。我不知道出了什么问题。这是我写的代码:
import nltk
from nltk import treebank
for filename in nltk.corpus.treebank.fileids():
files=open(filename,'r')
file=files.read()
for sentence in file:
a=sentence.index('to')
#a=the position in which 'to' appears
b=sentence.index('give')
#b=the position in which 'give' appears
mylist=[]
if a!=-1 and b!=-1:
#if a and b exist simultaneously
mylist.append(sentence)
print(mylist)
new_file_name='List'+'.txt'
#create a new file
file=open(new_file_name,'w')
for sentence in file:
file.write(sentence)
file.write('\n')
file.close()
有人可以帮助我吗?