python - Read words from .txt, and count for each words -


I wonder how to read fscanf such as character string, I need to read in the word all, in .txt I need a count for each word.

  with collectwords = collections.defaultdict (int) Open ('DatoSO.txt', 'r') as filetxt: For characters in line: = str < = "V" = "v" "  

In this way, I can not have difficulty reading the final word.

If you are using Python and GT, you can use collections.counter You can use. ; = 2.7

It combines several ways like 'most unusual', which can be useful in this type of application.

) With f: for the line in f: c.update (line print 'most common:' in the letter, c.most_common (3)): print '% s:% 7d'% (letter, count) < / Code>

- Although it counts the letter instead of the terminology, in the c.update line, you can change the line.rstrip () .com line.split () and perhaps want to replace some code to get rid of punctuation.

Edit: Here is the fastest way to remove punctuation:

  import import import string c = archive counter () Open ('Data SourcePT', 'RT') As F: f: c.update (for line transcript (string .maketrans ("", "", string.punctuation) .split))   

(The following question Borrowed from)

Comments