import re
with open('test.txt','r') as f:
str1 = f.read()
match_rex = re.compile(r'.*?(ADJECTIVE).*?(NOUN).*?(VERB).*?(NOUN).*')
match_object = match_rex.search(str1)
#print(match_object.groups())
for i in match_object.groups():
#print(i)
word_rex = re.compile(i)
str1 = word_rex.sub(input(f'Enter an {i.lower()}:\n'),str1,count=1)
with open('test2.txt','w') as f:
f.write(str1)
print(str1)
Python 小程序——疯狂填词
最新推荐文章于 2025-02-08 09:43:16 发布
本文介绍了使用Python中的re模块,通过正则表达式搜索并替换文本文件中特定词性的单词(如形容词、名词、动词和名词),实现文本内容的修改。
133

被折叠的 条评论
为什么被折叠?



