Count Words in a String (upgrade version)

Topics requirements:

read these strings in from a text file and generate a summary.

 

import os
def count_words(file):
    try:
        F = Open (File, ' R & lt ' , encoding = ' UTF-. 8 ' )
     the except IOError AS S:
         Print (S)
         return None
     # the try, the except statement: to catch exceptions, try: try code execution, except: emergence error processing 
    the try :
        buffer = f.read()
    except:
        print('Read File Error!')
        return None
    (filepath, filename) = os.path.split (File)
     # use os module path.split function to get the incoming file path name and file name 
    f.close ()
    Buffer = buffer.lower ()
     # replace all lowercase, so as not to deal with the back of the case in accordance with the different characters 
    words = buffer.split ( '  ' )
     # Press the space split text for the English word, it is not known how to split a word a word Character segmentation obtained 
    Counts = {}
     # (), [], {} represent the tuples, lists, dictionaries, there is created an empty dictionary results received after the loop counter 
    sumcount = 0
     for Word in words:
        Counts [Word] = counts.get (Word, 0) +. 1 
        sumcount = sumcount. 1 +
     # loop counter: 
    # dict.get (Key, default = None) 
    # Key - the dictionary to find the key. 
    # Default - if the specified key does not exist, return the default value. 
    # When the word is not in words, the return value is 0, when the words in a word, and +1, thereby accumulating counts. 
    = items List (counts.items ())
    the Items.Sort (Key = the lambda X: X [. 1], Reverse = True)
     # The dictionary sorted by the value of value 
    FileResult = Open (the os.path.join (filepath, " the result.txt " ), ' W ' )
     # under the incoming path to the file, create a new result file with writable attribute 
    for i in the Range (5 ):
        word, count = items[i]
        fileresult.write(word)
        # F.write can only pass one value, there is no more concise wording of it? Comments welcome 
        fileresult.write ( ' \ the n- ' )
        fileresult.write(str(count))
        fileresult.write('\n')


# Call the function: 
Count_words ( " C: \\ cccc the Users \\ \\ \\ hamlet.txt Desktop " )

The original file is:

Oh they say people come say people go
This particular diamond was extra special
And though you might be gone and the world may not know
Still I see you celestial
When I should but I can't let you go
But when I'm cold cold
When I'm cold cold
There's a light that you give me when I'm in shadow
There's a feeling within me an everglow
Like brothers in blood sisters who ride
And we swore on that night we'd be friends 'til we die
But the changing of winds and the way waters flow
Life is short as the falling of snow
And now I'm gonna miss you I know
But when I'm cold cold
In water rolled salt
I know you're with with me and the way you will show
And you're with me wherever I go
And you give me this feeling this everglow
Oh what I wouldn't give for just a moment to hold
Becasue I live for this feeling this everglow
So if you love someone you should let them know
Oh the light that you give me will everglow

 

The results are:

you
10
i
7
the
6
me
6
i'm
5

Guess you like

Origin www.cnblogs.com/YlnChen/p/12586607.html