现在很多公司都会有属于自己公司的专属邮箱。但是邮箱重复确实一个很麻烦的事情。
业务需求如下:
自动解析姓名为拼音姓和拼音名,由拼音姓和拼音名组装一个邮箱,如果组装的邮箱重复则需要加上数字,数据累计向上。
from xpinyin import Pinyin# 邮箱后缀
email_suffix = '@163.com'
# 历史邮箱
history_email = ['wenfeng.gu@163.com','wen.gu@163.com','wenfeng.gu1@163.com','wenfeng.gu2@163.com','wenfeng.guo@163.com',
]compound_surname = ['欧阳', '太史', '端木', '上官', '司马', '东方', '独孤', '南宫', '万俟', '闻人', '夏侯', '诸葛', '尉迟', '公羊','赫连', '澹台', '皇甫', '宗政', '濮阳', '公冶', '太叔', '申屠', '公孙', '慕容', '仲孙', '钟离', '长孙', '宇文','司徒', '鲜于', '司空', '闾丘', '子车', '亓官', '司寇', '巫马', '公西', '颛孙', '壤驷', '公良', '漆雕', '乐正','宰父', '谷梁', '拓跋', '夹谷', '轩辕', '令狐', '段干', '百里', '呼延', '东郭', '南门', '羊舌', '微生', '公户','公玉', '公仪', '梁丘', '公仲', '公上', '公门', '公山', '公坚', '左丘', '公伯', '西门', '公祖', '第五', '公乘','贯丘', '公皙', '南荣', '东里', '东宫', '仲长', '子书', '子桑', '即墨', '达奚', '褚师', '吴铭'
]# 把姓名解析为拼音
name = '谷文峰'# 仅支持2-4个字的姓名
if len(name) <= 4:# 判断是否复姓if name[:2] in compound_surname:last_name = name[:2]first_name = name[2:]else:last_name = name[:1]first_name = name[1:]pinyin = Pinyin()en_last_name = pinyin.get_pinyin(last_name, splitter='').capitalize().lower()en_first_name = pinyin.get_pinyin(first_name, splitter='').capitalize().lower()email = en_first_name + '.' + en_last_name# 先把历史的邮箱后缀全部去掉_history_email = [i.split('@')[0] for i in history_email]print(_history_email)# 判断是否重复, 如果重复, 则在后面加数字,如果已有数字, 则加1def is_duplicate(email, _history_email):if email in _history_email:return Trueelse:return Falsewhile is_duplicate(email, _history_email):if email[-1].isdigit():email = email[:-1] + str(int(email[-1]) + 1)else:email = email + '1'print(email + email_suffix)
以上代码可实现此功能。