python编写程序,打开一个已经存在的文件c:\demo.txt,读出文件中的内容,判断该文件中某些给定关键字如“组织”出现的次数。
时间: 2024-02-11 14:08:01 浏览: 26
好的,类似上一个问题,以下是样例代码:
```python
# 打开文件
with open('c:\\demo.txt', 'r') as file:
# 读取文件内容
content = file.read()
# 关键字列表
keywords = ['组织']
# 统计关键字出现次数
count = 0
for keyword in keywords:
count += content.count(keyword)
print('关键字出现次数:', count)
```
不同之处在于,需要使用`c:\\demo.txt`来表示Windows系统中的文件路径。其他部分与上一个问题的代码相同。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)