python电话 邮箱正则匹配清洗

</pre>做数据清洗时候,难免会碰到有些客户电话,邮箱乱填写,一看就明显不正确,用到python写个正则表达式可以进行快速的清洗</p><p>废话不多说,上代码:</p><p><pre name="code" class="python">#encoding:utf-8
'''
Created on 2015年9月18日

@author: ZHOUMEIXU204
'''

import MySQLdb
import  re
import pandas  as pd
con=MySQLdb.connect(host="10.10.109.62", port=1333, user="zhoumeixu204", \
                     passwd="zhoumeixu204@123456!", db="ubs", use_unicode=True, charset="utf8")

result=pd.read_sql("SELECT cust_id , USR_MP,USR_EMAIL FROM ubs.usr_oper_info;",con)
def    mobile_match(x):
    pattern=re.compile(r'1[3,5,4,7,8]{1}[0-9]{9}')
    match=pattern.match(x)
    if match:
        return   True
    else:
        return False
result['mobile_judge']=result['USR_MP'].map(mobile_match)

def  email_match(x):
    if x.strip().endswith('pingan.com.cn'):
        patten=re.compile(r'[a-zA-Z]{1,15}[0-9]{3}\@pingan\.com\.cn')
        match=patten.match(x)
        if match:
            return True
        else:
            return  False
    else:
        if len(x) > 7:
            if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", x) != None:
                return True
            else:
                return False
        else:
            return False         
result['usr_email_judge']=result['USR_EMAIL'].map(email_match)
result.to_excel(u"D:\\匹配结果.xls",index=False)
print(result.head())
con.close()


实际都是一些技巧,记录一些,免得要用时候又要查找

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值