import os
with open('DouJuan', encoding='utf-8') as file_obj:
contents = file_obj.read()
print(contents)
string = contents.lower()
string = string.replace("\n"," ")
newstring = ""
for i in string:
if((i >= 'a' and i <='z') or i == ' '):
newstring += i
words = newstring.split(sep=' ')
FunctionWord = [""," ","the","a","an","and","of","to","in","at","but","as","with","or"]
statisticsWords = ["start"]
for i in words:
if(not(i in FunctionWord)):
statisticsWords.append(i)
print(statisticsWords)
print(type(words))
wordsDict = {}
for i in statisticsWords:
if(not(i in wordsDict.keys())):
wordsDict[i] = 1
else:
wordsDict[i] += 1
print(wordsDict)
wordsDict = sorted(wordsDict.items(),key=lambda x:x[1],reverse=True)
statisticsDescirbe = ""
for i in wordsDict:
statisticsDescirbe += str(i[0]) + " : " + str(i[1]) + '\n'
print(statisticsDescirbe)
with open("/root/test1/XXXXXXXXXXXX","w") as file_object:
file_object.write(statisticsDescirbe)
print("done")
|