本项目采用的是https://github.com/Yixiaohan/show-me-the-code中所提供的练习项目,所有代码均为原创,转载请注明,谢谢
敏感词文本文件 filtered_words.txt,里面的内容为以下内容,当用户输入敏感词语时,则打印出 Freedom,否则打印出 Human Rights。
# -*- coding: utf-8 -*-
"""Created on Thu Jan 12 13:55:35 2017
@author: sky
"""
def trans_to_words():
type_in = raw_input(">")
judge_flag = False
fout = open('reslut.txt','w')
with open('1.txt') as f:
text = f.read().decode('gbk').encode('gbk')
for i in text.split("\n"):
if i in type_in:
judge_flag = True
if judge_flag:
print "Freedom"
fout.write('Freedom')
else:
print "Human Rights"
fout.write('Human Rights')
fout.close
if __name__ == "__main__":
trans_to_words()
注意:
里面涉及到汉字转码,可以参考http://www.111cn.net/phper/python/38513.htm
详细代码和结果,可以参考https://github.com/g8015108/exercise-for-python