File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import re
2+ lines_count = 0
3+ #words_count = 0
4+ chars_count = 0
5+ lines_list = []
6+ words_list = {}
7+ with open ('./test.txt' ,'r' ) as f :
8+ #http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820066616a77f826d876b46b9ac34cb5f34374f7a000
9+ for line in f :## 使用文件迭代器 , 每次只读取和显示一行
10+ #http://blog.csdn.net/mvpme82/article/details/5674200
11+ lines_count += 1
12+ chars_count += len (line )#算空格
13+ #正则表达式http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832260566c26442c671fa489ebc6fe85badda25cd000
14+ lines_list = re .findall (r'[a-zA-Z\-]+' ,line )
15+ print lines_list
16+ for i in lines_list :
17+ #words_count += 1
18+ if i not in words_list :
19+ words_list [i ] = 1
20+ else :
21+ words_list [i ] += 1
22+
23+ print 'lines_count:' ,lines_count
24+ print 'chars_count:' ,chars_count
25+ print 'words_count:' ,len (words_list )#输出字典的元素个数
26+
27+ for word ,quantity in words_list .items ():
28+ print word ,'\' s number is ' ,quantity
29+ ## f.close()
Original file line number Diff line number Diff line change 1+ hello world!
2+ lujianjie
3+ ###
4+ ksdfksdkk*&
5+ ljj-wyj
6+
7+ lujianjie
8+ ###
9+ ksdfksdkk*&
10+ ljj-wyj lujianjie
11+ ###
12+ ksdfksdkk*&
13+ ljj-wyj
You can’t perform that action at this time.
0 commit comments