forked from Show-Me-the-Code/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0012.py
More file actions
33 lines (24 loc) · 1016 Bytes
/
0012.py
File metadata and controls
33 lines (24 loc) · 1016 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Source:https://github.com/Show-Me-the-Code/show-me-the-code
# Author:renzongxian
# Date:2014-12-22
# Python 3.4
"""
第 0012 题: 敏感词文本文件 filtered_words.txt,里面的内容 和 0011题一样,当用户输入敏感词语,则用 星号 * 替换,
例如当用户输入「北京是个好城市」,则变成「**是个好城市」。
"""
def filter_words(words):
# Read filtered words from file named 'filtered_words.txt'
file_object = open('filtered_words.txt', 'r')
filtered_words = []
for line in file_object:
filtered_words.append(line.strip('\n'))
file_object.close()
# Check if the input words include the filtered words and replace the filtered with '*'
for filtered_word in filtered_words:
if filtered_word in words:
words = words.replace(filtered_word, '*'*len(filtered_word))
print(words)
if __name__ == '__main__':
while True:
input_words = input('Input some words:')
filter_words(input_words)