python show-me-the-code 第0012题
来源:互联网 发布:学闽南话的软件 编辑:程序博客网 时间:2024/04/28 07:26
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
分析:正则匹配
代码如下:
#-*- coding: utf-8 -*-import redef check(input,filtered_words):result=inputfor i in filtered_words:if i in result: r=re.compile(i) result=r.sub('*'*len(i.decode('utf-8')),result)return resultreturn inputfile = open('filtered_words.txt')filtered_words=[line.replace('\n','') for line in file] print check('lovely boy', filtered_words)print check('程序员在上班。', filtered_words)print check('我妈妈是农民。', filtered_words)print check('北京欢迎你',filtered_words)print check('hello python!',filtered_words)结果如下:
****ly boy***在上班。我妈妈是农民。**欢迎你hello python!
0 0
- python show-me-the-code 第0012题
- python show-me-the-code 第0004
- Python Show-Me-the-Code 第 0002 题 MySQL操作
- Python Show-Me-the-Code 第 0003 题 Redis操作
- Python Show-Me-the-Code 第 0004 题 统计单词
- Python Show-Me-the-Code 第 0021题 密码加密
- Python Show-Me-the-Code 第 0001题
- Python Show-Me-the-Code 第 0002题
- python show-me-the-code 第0010题
- python show-me-the-code 第0011题
- Python Show-Me-the-Code 第 0012 题 替换敏感词
- Python Show-Me-the-Code 第 0000 题 图片右上角加数字
- Python Show-Me-the-Code 第 0001 题 生成激活码
- Python Show-Me-the-Code 第 0005 题 批量图片处理
- Python Show-Me-the-Code 第 0006 题 最重要的词
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- Python Show-Me-the-Code 第 0010 题 生成验证码图片
- hdoj 整数对 1271 (技巧&数学)
- c++ assert() 使用方法
- shell中字符串操作函数
- C语言第二篇:分支程序设计(正差值、三数最大值、发工资)
- Win10删除内置应用
- python show-me-the-code 第0012题
- oracle执行计划详解
- 蓝桥杯 排它平方数 dfs
- Linux学习笔记--find命令(文件搜索命令)
- Ajax及传统Ajax详解
- 模式分类之贝叶斯决策入门
- 【运动控制】运动控制技术与机器视觉应用的感受
- 搜索引擎倒排索引表压缩:gamma编码、Golomb编码
- Retinex算法详解