python 参议院文本预处理的一维数组的间隔空间

#!/usr/bin/python
import re
def pre_process_msg ( msgIn ):
    if msgIn=="":
        return "msgIn_Input_Error,should'nt Null, it is Strings"
    else:
         #1 trim
         msg = msgIn
         msg = msg.strip()
         #2 process msg internal special char replace with “ ”
         dst_replace_pattern1 = re.compile('\n')
         msg = dst_replace_pattern1.sub(" ",msg)
         dst_replace_pattern1 = re.compile('\r')
         msg = dst_replace_pattern1.sub(" ",msg)
         dst_replace_pattern1 = re.compile('\t')
         msg = dst_replace_pattern1.sub(" ",msg)
         #3 one or more space replaced with one space,to form srings with " " internal
         result=""
         result=re.sub(" {1,}", " ", msg)
         msg=result.strip()
         print "'"+msg+"'"
         return msg

版权声明:本文博主原创文章,博客,未经同意不得转载。

posted @ 2015-10-23 12:55  hrhguanli  阅读(234)  评论(0编辑  收藏  举报