当前位置:首页 > Python教程 > python技巧

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

原文:http://blog.csdn.net/youcharming/article/details/46391343


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐