Python text statistics function of the journey to the monkey with the word statistics operation

Source: Internet
Author: User
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 () 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.