mnc_dict = {
"4121579+":"22851",
"4122579+":"22852",
}
import pandas as pd
import re
def match_phone_numbers(phone_numbers, regex_patterns):
results = []
results_mnc = []
for number in phone_numbers:
number = number.replace("'","")
matches = []
DEST_OPER_IDs = []
for pattern in regex_patterns:
if re.match(pattern, number):
matches.append(pattern)
DEST_OPER_IDs.append(mnc_dict.get(pattern))
if matches==[]:
matches = ['default']
DEST_OPER_IDs = ['22899']
results.append(matches)
results_mnc.append(DEST_OPER_IDs)
return results, results_mnc
# 示例输入数据
# phone_numbers = [
# '417996689121',
# '41799672434',
# '41799672504',
#
# ]
phone_numbers = pd.read_excel(r'phoneNBs.xlsx', dtype=str)['phoneNB_E164']
regex_patterns = mnc_dict.keys()
# 调用函数进行匹配
matched_results, results_mnc = match_phone_numbers(phone_numbers, regex_patterns)
# 创建一个字典,将列表数据作为值,列名作为键
data = {'Column1': phone_numbers, 'Column2': matched_results, 'Column3': results_mnc}
# 创建一个DataFrame
df = pd.DataFrame(data)
# 导出为Excel文件
df.to_excel('output.xlsx', index=False)
# 输出匹配结果
for i, number in enumerate(phone_numbers):
print(f'{number}: {matched_results[i]}: {results_mnc[i]}')
给定一个电话号码列表,以及多条数字正则规则,输出每个电话号码匹配到的数字正则规则结果。请按此提供python代码。
最新推荐文章于 2024-11-02 16:28:26 发布