正则提取字符串中的mail地址 配合excel效果更佳

import xlrd, re
from datetime import datetime
from xlrd import xldate_as_tuple

xls_path = r"E:\工作相关\收件人員信息.xlsx"
data = xlrd.open_workbook(xls_path)
table = data.sheet_by_name("BU收件人員")

def get_mail_list(sheet_name):
    table = data.sheet_by_name(sheet_name)
    nrows = table.nrows
    ncols = table.ncols
    print(nrows, ncols)
    for i in range(1, nrows):
        bu_dict = {}
        name = table.cell(i, 0).value.strip().replace('次群报表', '')
        to = table.cell(i, 1).value.strip().replace('次群报表', '')
        cc = table.cell(i, 2).value.strip().replace('次群报表', '')
        to = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", to)
        cc = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", cc)
        # print(name)
        # print(to)
        # print(cc)
        bu_dict['to'] = to
        bu_dict['cc'] = cc
        mail_info[name] = bu_dict

sheet_list = ["群收件人員", "BU收件人員"]

mail_info = {}
for sheet_name in sheet_list:
    get_mail_list(sheet_name)
print(mail_info)

for k,v in mail_info.items():
    print(k, v)

print(len(mail_info))
import re

my_str = "Hi my name is John and email address is john.doe@somecompany.co.uk and my friend's email is jane_doe124@gmail.com"
emails = re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)", my_str)

for mail in an email:
print(mail)


https://www.cainiaojc.com/note/qa0mdr.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值