[python每日一练]--0011:敏感词过滤 type1

题目链接:https://github.com/Show-Me-the-Code/show-me-the-code
代码github链接:https://github.com/wjsaya/python_spider_learn/tree/master/python_daily
个人博客地址:https://wjsaya.github.io
第 0011 题: 敏感词文本文件 filtered_words.txt,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。

北京
程序员
公务员
...

思路:

  1. 从文件解析敏感词。
  2. 根据敏感词决定输出。

敏感词列表(filtered_words.txt)

北京
程序员
公务员
领导
牛比
牛逼
你娘
你妈
love
sex
jiangge

代码:

#!/usr/bin/env python3
# -*- coding: utf-8 -*- 
# @Author:  wjsaya(http://www.wjsaya.top) 
# @Date:    2018-08-10 10:33:32 
# @Last Modified by:    wjsaya(http://www.wjsaya.top) 
# @Last Modified time:  2018-08-10 12:33:32 

def get_dirty(fileName=''):
    '''解析文件获取敏感词
    '''
    with open (fileName, 'r', encoding='utf-8') as f:
        re = f.readlines()

    for i in range(len(re)):
        re[i] = re[i].strip('\n')

    return(re)

def fliter(dirty_dict):
    '''过滤敏感词
    '''
    instr = input("不要输入敏感词哦:")
    for i in dirty_dict:
        if (instr == i):
            print('Freedom')
            return 1

    print('Human Rights')
    return 0


if __name__ == '__main__':
    file = 'filtered_words.txt'
    dirty_dict = get_dirty(fileName=file)
    while(1):
        fliter(dirty_dict)

效果图:

0011

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值