This article mainly introduces the Python text statistics function of the journey to the monkey with the word statistics operation, combined with examples of Python text reading, traversal, statistics and other related operations skills, the need for friends can refer to the next
This article describes the Python text statistics function of the journey to the monkey with the word statistics operation. Share to everyone for your reference, as follows:
First, the data
Xyj.txt, "Journey to the Monkey" text, 2.2MB
Tribute to Master Wu Chengen, line 4020 (segment)
II. Objectives
In the "Journey to the Monkey":
1. How many different Chinese characters have appeared in a total;
2. How many times each character has appeared;
3. What are the most frequently occurring Chinese characters?
Third, the content of the relevant:
1. Read the document;
2. Use of dictionaries;
3. Sorting of dictionaries;
4. Writing files
Four, the effect
Five, source code
# coding:utf8import sysreload (SYS) sys.setdefaultencoding ("UTF8") fr = open (' Xyj.txt ', ' R ') characters = []stat = {}for lines in fr: # Remove Blank lines on each line = Line.strip () # Skip the round if it's a blank row if Len (line) = = 0:continue # Convert text to Unicode for easy processing kanji line = Unicode (lines) # traverse every word of the row for X in xrange (0, Len): # Remove punctuation and whitespace if line[x] I n [' ', ' ', ' \ t ', ' \ n ', '. ', ', ', ' (', ') ', ' (', ') ', ': ', '-', '? ', '! ', ' ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' ... ': Continue # Not yet recorded in the characters if not line[x] in characters:characte Rs.append (Line[x]) # not yet recorded in stat if not Stat.has_key (Line[x]): stat[line[x]] = 0 # Kanji occurrences plus 1 stat[line[x] + = 1print len (characters) print len (stat) # lambda generates a temporary function # D represents a dictionary of each pair of key-value pairs, d[0] is key,d[1] is value# reverse is true to indicate descending sort stat = Sorted (Stat.items (), Key=lambda d:d[1], reverse=true) FW = open (' Result.csv ', ' W ') for item in stat: # before string concatenation, you need to convert int to St R Fw.write (item[0] + ', ' + str (item[1]) + ' \ n ') fr.close () fw.close ()