利用Python编程提取身份证的信息

例如从身份证信息提取隶属地区名,出生日期等。第二代身份证的位数为18位。

源码如下:

import re

# Sample 18-bit ID card number containing the birth date
id_card_number = "51142119991021155x"

# Define the regex pattern to extract the birth date
pattern = r'\d{4}(?:0[1-9]|1[0-2])(?:0[1-9]|[12][0-9]|3[01])'

# Search for the birth date in the ID card number
match = re.search(pattern, id_card_number)

# 在csdn搜到的文本信息,部分信息省略...
area_code = '''
110000 北京市       
 110101    东城区       
 110102    西城区       
 .....
 .....
 659006    铁门关市       
 659007    双河市       
 659008    可克达拉市       
 659009    昆玉市       
 659010    胡杨河市       
 710000 台湾省       
 810000 香港特别行政区       
 820000 澳门特别行政区 
'''
# 定义一个匹配区号和地区名的正则
pattern = re.compile('(\d+)\s+(\w+)')
# 将上面的字符串转成字典格式,键为区号,值为地区名
area_code_dict = dict(pattern.findall(area_code))


# 提取出生日期
if match:
    birth_date = match.group(0)
    print(f"出生日期: {birth_date}")
else:
    print("No birth date found in the ID card number.")

# 提取地区号
pattern1 = re.compile('^\d{6}')
match = pattern1.match(id_card_number)
if match:
    print('地区号为:', match.group(0))
    print('身份隶属于:',area_code_dict[match.group(0)])
else:
    print("No area code found in the ID card number.")

运行结果如下:

出生日期: 19991021
地区号为: 511421
身份隶属于: 仁寿县

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值