The existing plain text titled Test.txt, which counts the number of occurrences of the word.
Content of Test.txt:
I have the application Someday Oneday day Demo
I have some one coma ideal naive I
Code implemented in Python:
Import re
Count = {}
f = open (' Test ', ' R ')
b = F.read ()
#print b
cd = Re.split (' [\\n]+ ', b) #注意split的用法
Print CD
For I in CD:
Count[i] = Count.get (i,0) + # Note the use of the Get () method
Print Count
Results obtained after executing the code:
[' I ', ' have ', ' has ', ' application ', ' Someday ', ' oneday ', ' Day ', ' demo ', ' I ', ' has ', ' some ', ' one ', ' coma ', ' ideal ', ' Nai ve ', ' I ']
{' Someday ': 1, ' I ': 3, ' demo ': 1, ' naive ': 1, ' some ': 1, ' One ': 1, ' Application ': 1, ' ideal ': 1, ' has ': 3, ' coma ': 1, ' on Eday ': 1, ' Day ': 1}
The shell implements the method as follows:
TR "" "\\n"
Run the result as
1 application
1 coma
1 day
1 Demo
3 have
5/
1 Ideal
1 naive
1 One
1 oneday
1 some
1 Someday
This article is from "Jason's blog" blog, please be sure to keep this source http://jason83.blog.51cto.com/12723827/1982168
Any plain text file in English, counting the number of words that appear in the language (Shell Python two languages are implemented)