用pytjon开发敏感词语过滤程序,提示用户输入内容,如果用户输入的内容中包含敏感的字符,则将内容替换为***
时间: 2023-08-19 13:04:55 浏览: 103
好的,下面是一个简单的 Python 程序,可以用来过滤输入内容中的敏感词汇:
```python
SENSITIVE_WORDS = ['敏感词1', '敏感词2', '敏感词3'] # 敏感词汇列表
def filter_sensitive_words(input_str):
"""
过滤输入内容中的敏感词汇
"""
for word in SENSITIVE_WORDS:
input_str = input_str.replace(word, '***')
return input_str
# 用户输入内容
input_str = input('请输入内容:')
# 过滤敏感词汇
filtered_str = filter_sensitive_words(input_str)
# 输出过滤后的内容
print(filtered_str)
```
在这个程序中,我们首先定义了一个包含敏感词汇的列表 `SENSITIVE_WORDS`。然后,我们定义了一个名为 `filter_sensitive_words` 的函数,它接受一个字符串作为参数,并将其中的敏感词汇替换为 `***`。函数中,我们使用了字符串的 `replace` 方法来替换文本中的敏感词汇。
接下来,我们提示用户输入内容,并调用 `filter_sensitive_words` 函数来过滤敏感词汇。最后,我们将过滤后的内容输出到屏幕上。
相关问题
题目:过滤字符 猫述:开发飘即词语过滤程序,提示用户输人内容 如果用户输人的内容中包含敏感的字符,则将内容普换为*** 要求:将“环境”和“特殊”替换为***
好的,我可以用 Python 编写这个程序。以下是代码示例:
```python
sensitive_words = ["环境", "特殊"] # 敏感词列表
def filter_words(text):
"""
过滤敏感词
"""
for word in sensitive_words:
if word in text:
text = text.replace(word, '***')
return text
if __name__ == '__main__':
user_input = input("请输入内容:")
filtered_text = filter_words(user_input)
print(filtered_text)
```
在这个程序中,我们首先定义了一个敏感词列表 `sensitive_words`。然后定义了一个 `filter_words` 函数,用于过滤用户输入中的敏感词。在函数中,我们遍历敏感词列表,如果发现某个敏感词出现在了用户输入中,就使用字符串的 `replace` 方法将其替换为 `***`。
最后,在 `if __name__ == '__main__'` 块中,我们获取用户输入,调用 `filter_words` 函数进行过滤,并输出过滤后的内容。
python过滤敏感词(要求:将输入语句中的敏感词语使用*替换。敏感词库集合 sensitive_words = {'敏感1','敏感2','敏感3','敏感4','敏感5','敏感6' })
在Python中,过滤敏感词通常可以通过字符串处理和列表推导式来实现。以下是一个简单的示例,展示了如何使用`sensitive_words`列表来替换输入语句中的敏感词:
```python
sensitive_words = {'敏感1', '敏感2', '敏感3', '敏感4', '敏感5', '敏感6'}
def filter_sensitive(input_text):
return ' '.join('*' if word in sensitive_words else word for word in input_text.split())
# 示例用法
input_string = "这是一个包含敏感1和敏感2的句子"
filtered_string = filter_sensitive(input_string)
print(filtered_string)
```
这个`filter_sensitive`函数会遍历输入文本中的每个单词,如果单词在敏感词集合中,则替换为星号;否则保持原样。运行这段代码后,输出将是`"这是一个包含*和*的句子"`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)