首页 > 解决方案 > 如何使用 re.sub 替换特定的迭代?

问题描述

我正在为一个项目制作一个 madlibs python 程序,但在替换输入中的特定单词时遇到了麻烦。

我想更改单个迭代,而不是所有迭代。放轻松,我是新手

# Run a loop to replace the words

print("Replacing Words...")

for loopCount in range(replacedNumber):
    currentReplace = replaceList[loopCount] 
    print(re.finditer(r'<.*?>',storyContent))
    print("Replacing", currentReplace)
    filteredReplaced = re.sub(r'<.*?>',currentReplace, storyContent, loopCount)

注意:仅用于调试的 print() 行

这是所有代码的链接

控制台输出

标签: python-3.7

解决方案


推荐阅读