This article describes how to determine the number of message repetitions in a text by using Python, and describes how to read text files and perform string operations by using Python, for more information about how to use Python to determine the number of message repetitions in a text, see the following example. We will share this with you for your reference. The details are as follows:
# Coding: gbk ''' Created on 2012-2-3 read text from the file, determine the number of duplicate @ author messages in the text, such as "message0" and "message123: administrator ''' import reif _ name _ = '_ main _': pattern = u "(message (\ d) +)" prog = re. compile (pattern) # read text from file f = open ("1003.txt"," r ") text = unicode (f. read () f. close () result = prog. findall (text) message_map = dict () redupicate_count = 0 for message in result: if message_map.has_key (message [0]) = True: print message [0], "is required plicate" redupicate_count + = 1 else: message_map [message [0] = 1; print "total used plicate message is", redupicate_count