Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- folderPath = "E:/_PythonSuli/0413/homeworks/"
- textfile = "The Adventures of Sherlock Holmes.txt"
- mydocument = folderPath + textfile
- with open(mydocument, "r") as f:
- mytext = str(f.read())
- if len(mytext) == 0:
- print("file is empty")
- else:
- numberOfWords = len(mytext.split(" ")) # ez így szebb!
- character = (len(mytext.replace(" ", ""))) #karakterek száma
- mytext2 = mytext.replace("\n", " ")
- listOfWords = [ word for word in mytext2.split(" ") if len(word)>3]
- freq = collections.Counter(listOfWords).most_common(3)
- statfile = "Szavak száma: {}\nKarakterek száma (szóköz nélkül): {}\nA három leggyakoribb " \
- "szó: {}, {}, {}".format(numberOfWords,character,freq[0][0],freq[1][0],freq[2][0])
- with open(folderPath + "statistics.txt", "w") as f:
- f.write(statfile)
- print(statfile)
Advertisement
Add Comment
Please, Sign In to add comment