python正则过滤文件指定邮箱地址的方法,python邮箱地址,Python正则的学习是


Python正则的学习是一个难点,但正则方法却是很有效的,可以匹配出任何你给定的条件。下边就是用python正则匹配方法,过滤掉文件中指定的邮箱地址。

需要用到的python模块有:python re正则、python os、python sys

#coding=utf8# 过滤掉域名为10个字符的邮箱import reimport osimport sysdef mail_filter(srcfile, pattern):    fin = open(srcfile, 'r')    for line in fin:        pat = re.compile(pattern)        m = pat.match(line)        # 没有匹配则输出        if not m:            print line,     fin.close()#www.iplaypy.comif __name__ == '__main__':    srcfile = 'in'    destfile = 'out'    # 重定向标准输出到文件    fout = open(destfile, 'w')    sys.stdout = fout    mail_filter(srcfile, r'\w{10}@\w*\.\w*')    fout.close()

编橙之家文章,

评论关闭