任务要求 打开一个文本 将所有含有 ADJECTIVE, NOUN, ADVERB OR VERB的地方,请用户输入一个ADJECTIVE, NOUN, ADVERB OR VERB,并填充文本,并且将由用户改过的文本输出并保存到一个新的文本
但是现在在copytext当中 符合正则表达式的格式的文字没有被取代,该怎么办???
#!python3
#madLibs - to attempt the user to type in adjective, noun, adverb and
#verb to replace the ADJECTIVE, NOUN, ADVERB OR VERB
import os,re,pyperclip
##open the text
#suppose there is a text named madLibs.txt
mad=open('madLibs.txt','r')
content=mad.readlines()
copytext=content
##find whether these specific words exist or not
typeRegex=re.compile(r'ADJECTIVE|ADVERB|NOUN|VERB',re.I)
##We need to run the loop through every sentence of the text
for i in range(len(content)):
for j in range(len(typeRegex.findall(content))):
##we want to get every word that we need to substitute
print('Enter an %s:'%(str(typeRegex.findall(content)[j])).lower())
temp=str(input())
print(temp)
##substitute with the word that user input
typeRegex.sub(r'%s'%(temp),copytext)
print(copytext)
print(copytext)
##Now we get a list of sentences with the words substituting to the input of user
madLibsNew=open('madLibsNew','w')
madLibsNew.write('This is the new text that some adjectives,adverbs, verbs and nouns that has been substitute from madLabs.txt')
madLibsNew.close()
for i in range(len(copytext)):
madLibsNew=open('madLibsNew','a')
madLibsNew.write(copytext)
print(copytext)
madLibsNew.close()