前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python开发_count()

python开发_count()

作者头像
Hongten
发布2018-09-13 12:54:15
3990
发布2018-09-13 12:54:15
举报
文章被收录于专栏:HongtenHongten

python中的count()函数,从字面上可以知道,他具有统计功能

下面来看看具体的demo:

功能:读取一个文件'c:\\test.txt',统计出该文件中出现字符'a'的次数

代码语言:javascript
复制
 1 #python open
 2 import os
 3 
 4 number = 0
 5 
 6 def get_number_of_char(filePath, c):
 7     '''
 8     from path, the system will get the file
 9     finally return the number char: c
10     '''
11     if os.path.exists(filePath):
12         global number
13         with open(filePath, 'r') as fp:
14             for line in fp:
15                 a = line.count(c)
16                 number = number + a
17     else:
18         print('the path:[{}] is not exist!'.format(filePath))
19 
20 def main():
21     file_path = 'c:\\test.txt'
22     get_number_of_char(file_path, 'a')
23     print(number)
24     
25 if __name__ == '__main__':
26     main()

运行效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-08-13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档