酷代码 AI
菜单
服务商
DeepSeek字节豆包讯飞星火
更多选项

Python代码实现:用星号替换文章中敏感词

以下是一个用Python编写的示例代码: ```python def filter_sensitive_words(sensitive_words, input_str): word_list = sensitive_words.split(",") for word in word_list: input_str = input_str.replace(word, '*' * len(word)) return input_str sensitive_words = "敏感词1,敏感词2,敏感词3" input_string = input("请输入文章内容:") filtered_string = filter_sensitive_words(sensitive_words, input_string) print(filtered_string) ``` 在这个代码中,`filter_sensitive_words`函数接收敏感词列表和用户输入的文章内容作为参数。它将敏感词列表分割成一个词语列表,然后遍历列表中的每个词语。使用`replace`方法,将文章中的敏感词替换为相同长度的星号(*)字符串。返回替换后的文章内容。 你需要将代码中的`敏感词1,敏感词2,敏感词3`替换为你实际的敏感词列表。运行代码时会提示你输入文章内容,然后输出被替换后的内容。 ######[AI写代码神器 | 614点数解答 | 2024-04-09 20:00:38]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]