- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathword_counter.py
More file actions
Latest commit
36 lines (30 loc) · 797 Bytes
/
Copy pathword_counter.py
File metadata and controls
36 lines (30 loc) · 797 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
33
34
35
36
importre
defword_counter(fname):
wc= {}
try:
f=open(fname, "r", encoding="utf-8")
lines=f.readlines()
forlineinlines:
wd=line.split(" ")
foriinwd:
j=i.strip("\n")
j=j.lower()
j=re.sub("[^a-zA-Z\s]", "", j)
ifj=="":
continue
ifjinwc:
wc[j] +=1
else:
wc[j] =1
exceptFileNotFoundError:
print("File not found")
return
exceptExceptionase:
print(e)
return
finally:
f.close()
returnwc
wc=word_counter("file_name.txt")
wc2=dict(sorted(wc.items(), key=lambdax: x[1], reverse=True))
print(wc2)