Python判断文本中消息重复次数的方法

故里桃花落无踪,小楼轩窗山影重。若见瘦湖病西子,凋零秋风映山红。正是荷花成熟的季节,一朵朵荷花含苞怒放,满田野里的荷田闪烁着金光,一簇簇的荷花有的带着水珠乍开,有的含苞吐蕾,有的像个含羞的少女似开非开。

本文实例讲述了Python判断文本中消息重复次数的方法。分享给大家供大家参考,具体如下:

#coding:gbk
'''
Created on 2012-2-3
从文件中读取文本,并判断文本中形如“message0”、“message123”这样的消息有多少条是重复的
@author: Administrator
'''
import re
if __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 reduplicate"
      redupicate_count += 1
    else :
      message_map[message[0]] = 1;
  print "total reduplicate message is ", redupicate_count

希望本文所述对大家Python程序设计有所帮助。

以上就是Python判断文本中消息重复次数的方法。考研,苦大仇深者必胜。更多关于Python判断文本中消息重复次数的方法请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战