Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用正则表达式在Python中构建mad libs程序_Python 3.x - Fatal编程技术网

Python 3.x 使用正则表达式在Python中构建mad libs程序

Python 3.x 使用正则表达式在Python中构建mad libs程序,python-3.x,Python 3.x,我是一名新的Python程序员,正在阅读这本书,用Python自动化那些无聊的东西。本章末尾的一个项目是构建一个mad libs程序。基于到目前为止所介绍的内容,我认为作者打算让我使用正则表达式 这是我的密码: #! python3 # # madlibs.py - reads a text file and let's the user add their own text # anywhere the words ADJECTIVE, NOUN, ADVERB, or VERB appear

我是一名新的Python程序员,正在阅读这本书,用Python自动化那些无聊的东西。本章末尾的一个项目是构建一个mad libs程序。基于到目前为止所介绍的内容,我认为作者打算让我使用正则表达式

这是我的密码:

#! python3
#
# madlibs.py - reads a text file and let's the user add their own text
# anywhere the words ADJECTIVE, NOUN, ADVERB, or VERB appear in the text
# file.

import sys, re, copy

# open text file, save text to variable

if len(sys.argv) == 2:
    print('Opening text file...')
    textSource = open(sys.argv[1])
    textContent = textSource.read()
    textSource.close()
else:
    print('Usage: madlibs.py <textSource>')

# locate instances of keywords

keywordRegex = re.compile(r'ADJECTIVE|NOUN|ADVERB|VERB', re.I)
matches = keywordRegex.findall(textContent)

# prompt user to replace keywords with their own input

answers = copy.copy(matches)

for i in range(len(answers)):
    answers[i] = input()

# create a new text file with the end result

for i in range(len(matches)):
    findMatch = re.compile(matches[i])
    textContent = findMatch.sub(answers[i], textContent)

print(textContent)

textEdited = open('madlibbed.txt', 'w')
textEdited.write(textContent)
textEdited.close()
#!蟒蛇3
#
#py-读取一个文本文件,让用户添加自己的文本
#文本中出现形容词、名词、副词或动词的任何地方
#文件。
导入系统、重新导入、复制
#打开文本文件,将文本保存到变量
如果len(sys.argv)==2:
打印('打开文本文件…')
textSource=open(sys.argv[1])
textContent=textSource.read()
textSource.close()
其他:
打印('用法:madlibs.py')
#定位关键字的实例
关键字regex=re.compile(r'形容词|名词|副词|动词',re.I)
matches=keywordRegex.findall(textContent)
#提示用户用自己的输入替换关键字
答案=复制。复制(匹配项)
对于范围内的i(len(答案)):
答案[i]=输入()
#创建带有最终结果的新文本文件
对于范围内的i(len(匹配)):
findMatch=re.compile(匹配[i])
textContent=findMatch.sub(答案[i],textContent)
打印(文本内容)
text编辑=打开('madlibed.txt','w')
textcedited.write(textContent)
textdedited.close()
我用于textSource的输入是一个文本文件,其内容如下:

这是测试源文件。它里面有关键词形容词,还有关键词名词。此外,它还有一个名词实例,然后是一个副词实例


我的问题是findMatch.sub方法正在同时替换NOUN的两个实例。我知道sub()方法就是这样工作的,但是我很难想出一个简单的方法来解决它。如何设计此程序,使其一次只针对并替换一个关键字?我不希望所有名词都替换为同一个单词,而是根据用户键入它们的顺序替换不同的单词。

您只需将关键字参数count设置为sub,这样它就不会再替换您设置的词

textContent = findMatch.sub(answers[i], textContent, count=1)

有关更多详细信息,请参见

您只需将关键字参数count设置为sub,这样它就不会再替换您设置的关键字参数

textContent = findMatch.sub(answers[i], textContent, count=1)
有关更多详细信息,请参见thodnev的答案,不过有时最好先标记字符串,然后用这些部分构建一个新字符串

如果您的字符串为:

textContent = 'This is the test source file. It has the keyword ADJECTIVE in it, as well as the keyword NOUN. Also, it has another instance of NOUN and then one of ADVERB.'
然后,您可以使用
re.finditer
执行以下操作:

for it in re.finditer(r'ADJECTIVE|NOUN|ADVERB|VERB', textContent):
    print(it.span(), it.group())
给予

您可以将此信息与子字符串一起使用,以您想要的方式构建一个新字符串。

thodnev的答案是有效的,但有时您最好先对字符串进行标记,然后使用部分构建一个新字符串

如果您的字符串为:

textContent = 'This is the test source file. It has the keyword ADJECTIVE in it, as well as the keyword NOUN. Also, it has another instance of NOUN and then one of ADVERB.'
然后,您可以使用
re.finditer
执行以下操作:

for it in re.finditer(r'ADJECTIVE|NOUN|ADVERB|VERB', textContent):
    print(it.span(), it.group())
给予


您可以将此信息与子字符串一起使用,以您想要的方式构建新字符串。

最好使用
sys.exit('Usage:madlibs.py')
而不是
print('Usage:madlibs.py')
最好使用
sys.exit('Usage:madlibs.py')
而不是
print('Usage:madlibs.py')
谢谢!我不知道sub的count参数。应该对文档进行更深入的研究。谢谢!我不知道sub的count参数。我应该更深入地研究一下文档。