python语言-----------------身份证信息提前验证
##截取身份证信息,判断身份证上面的具体信息:
上代码:
import re
def extract_id_card_info(id_card):
# 匹配身份证号码并提取出生日期和顺序号
match = re.match(r'(\d{6})(\d{4})(\d{2})(\d{2})\d{2}(\d{1})(\d|X)', id_card)
if match is None:
return None
area_code, birth_year, birth_month, birth_day, order_code, gender_code = match.groups()
# 计算性别
gender = '男' if int(gender_code) % 2 == 1 else '女'
# 拼接出生日期
birth_date = '{}年{}月{}日'.format(birth_year, birth_month, birth_day)
# 返回提取出来的信息
return {
'id_card': id_card,
'area_code': area_code,
'birth_date': birth_date,
'gender': gender,
'order_code': order_code,
'gender_code': gender_code,
}
# 测试提取函数
id_card = '320723199105050011'
info = extract_id_card_info(id_card)
print(info)
实现效果如下: