用python编写词组“你”,“我”,“1”存储在一个文件中,另有一段话,我们在玩耍。存储在另一个文件中,将该段话中出现的“你”,“我”,“1”字符用“*”替换后另存为一个文件
时间: 2023-11-18 15:05:38 浏览: 21
以下是实现代码:
```python
# 读取存储了“你”,“我”,“1”的文件
with open("words.txt", "r") as f:
words = f.read().split()
# 读取含有“你”,“我”,“1”的段落
with open("paragraph.txt", "r") as f:
paragraph = f.read()
# 将段落中的“你”,“我”,“1”替换为“*”
for word in words:
paragraph = paragraph.replace(word, "*")
# 保存替换后的段落
with open("replaced_paragraph.txt", "w") as f:
f.write(paragraph)
```
假设存储了“你”,“我”,“1”的文件名为`words.txt`,存储了含有“你”,“我”,“1”的段落的文件名为`paragraph.txt`,替换后的段落将保存在`replaced_paragraph.txt`中。
阅读全文