写在前面

用python实现快速从身份证中explore尽量多的的信息,找了很多资料,发现并不全。参考已有的分享和其他信息自己写了。

能提取的信息:
出生日期、年龄、生肖、星座、省份、城市、区县、归属地地址、城市等级(2019划分标准)

id_card_area.xlsx 含省份、城市、区县、归属地地址
city_level.xlsx 含城市等级
两个文件可根据时间更新

代码

import pandas as pd
import numpy as np
area_info = pd.read_excel('id_card_area.xlsx', header = 0)
area_info = area_info.set_index('district_code')
city_level_2019 = pd.read_excel('city_level.xlsx', header = 0)
city_level_2019 = city_level_2019.set_index('city')class GetIdInformation(object):def __init__(self,id):self.id = idself.birth_year = int(self.id[6:10])self.birth_month = int(self.id[10:12])self.birth_day = int(self.id[12:14])self.address_code = int(self.id[0:6])def get_birthday(self):"""通过身份证号获取出生日期"""birthday = "{0}-{1}-{2}".format(self.birth_year, self.birth_month, self.birth_day)return birthdaydef get_sex(self):"""男生:1 女生:0"""num = int(self.id[16:17])if num % 2 == 0:return 0else:return 1def get_age(self):"""通过身份证号获取年龄"""now = (datetime.datetime.now() + datetime.timedelta(days=1))year = now.yearmonth = now.monthday = now.dayif year == self.birth_year:return 0else:if self.birth_month > month or (self.birth_month == month and self.birth_day > day):return year - self.birth_year - 1else:return year - self.birth_yeardef get_zodiac(self):  """通过身份证号获取生肖"""  start_year = 1901zodiac_interval = (int(self.birth_year) - start_year) % 12if zodiac_interval == 1 or zodiac_interval == -11:return '鼠'if zodiac_interval == 0:return '牛'if zodiac_interval == 11 or zodiac_interval == -1:return '虎'if zodiac_interval == 10 or zodiac_interval == -2:return '兔'if zodiac_interval == 9 or zodiac_interval == -3:return '龙'if zodiac_interval == 8 or zodiac_interval == -4:return '蛇'if zodiac_interval == 7 or zodiac_interval == -5:return '马'if zodiac_interval == 6 or zodiac_interval == -6:return '羊'if zodiac_interval == 5 or zodiac_interval == -7:return '猴'if zodiac_interval == 4 or zodiac_interval == -8:return '鸡'if zodiac_interval == 3 or zodiac_interval == -9:return '狗'if zodiac_interval == 2 or zodiac_interval == -10:return '猪'else:return np.nandef get_starsign(self):  """通过身份证号获取星座"""    if((self.birth_month== 1 and self.birth_day > 19) or (self.birth_month== 2 and self.birth_day <= 18)):return "水瓶座"if ((self.birth_month== 2 and self.birth_day > 18) or (self.birth_month== 3 and self.birth_day <= 20)):return "双鱼座"if ((self.birth_month== 3 and self.birth_day > 20) or (self.birth_month== 4 and self.birth_day <= 19)):return "白羊座"if ((self.birth_month== 4 and self.birth_day > 19) or (self.birth_month== 5 and self.birth_day <= 20)):return "金牛座"if ((self.birth_month== 5 and self.birth_day > 20) or (self.birth_month== 6 and self.birth_day <= 21)):return "双子座"if ((self.birth_month== 6 and self.birth_day > 21) or (self.birth_month== 7 and self.birth_day <= 22)):return "巨蟹座"if ((self.birth_month== 7 and self.birth_day > 22) or (self.birth_month== 8 and self.birth_day <= 22)):return "狮子座"if ((self.birth_month== 8 and self.birth_day > 22) or (self.birth_month== 9 and self.birth_day <= 22)):return "处女座"if ((self.birth_month== 9 and self.birth_day > 22) or (self.birth_month== 10 and self.birth_day <= 23)):return "天秤座"if ((self.birth_month== 10 and self.birth_day > 23) or (self.birth_month== 11 and self.birth_day <= 22)):return "天蝎座"if ((self.birth_month== 11 and self.birth_day > 22) or (self.birth_month== 12 and self.birth_day <= 21)):return "射手座"if ((self.birth_month== 12 and self.birth_day > 21) or (self.birth_month== 1 and self.birth_day <= 19)):return "魔羯座"else:return np.nan def get_province_only(self):  """通过身份证号获取省份"""    province_code = int(self.id[0:2])province_dict = {11 : "北京",12 : "天津",13 : "河北",14 : "山西",15 : "内蒙古",21 : "辽宁",22 : "吉林",23 : "黑龙江",31 : "上海",32 : "江苏",33 : "浙江",34 : "安徽",35 : "福建",36 : "江西",37 : "山东",41 : "河南",42 : "湖北",43 : "湖南",44 : "广东",45 : "广西",46 : "海南",50 : "重庆",51 : "四川",52 : "贵州",53 : "云南",54 : "西藏",61 : "陕西",62 : "甘肃",63 : "青海",64 : "宁夏",65 : "新疆",71 : "台湾",81 : "香港",82 : "澳门",91 : "国外"}if province_dict[province_code] is not None:return province_dict[province_code]else:return np.nandef get_province(self):  """通过身份证号获取省份"""    province_dict = area_info.to_dict()['province']if self.address_code in  list(province_dict.keys()):return province_dict[self.address_code]else:return np.nandef get_city(self):  """通过身份证号获取城市"""    city_dict = area_info.to_dict()['city']if self.address_code in  list(city_dict.keys()):return city_dict[self.address_code]else:return np.nandef get_district(self):  """通过身份证号获取区/县"""    district_dict = area_info.to_dict()['district']if self.address_code in  list(district_dict.keys()):return district_dict[self.address_code]else:return np.nandef get_address(self):  """通过身份证号获取完整归属地"""    address_dict = area_info.to_dict()['all']if self.address_code in  list(address_dict.keys()):return address_dict[self.address_code]else:return np.nandef get_city_level(self):"""通过身份证号获取城市等级"""city_dict = area_info.to_dict()['city']city_level_dict = city_level_2019.to_dict()['city_level']if self.address_code in  list(city_dict.keys()):city_name =  city_dict[self.address_code]if city_name in list(city_level_dict.keys()):return city_level_dict[city_name]else:return  '其他'else:return np.nanGetIdInformation('111111111111111111').get_city_level()#df['province'] = df.id_number.apply(lambda x:GetIdInformation(x).get_province)        

Python 从身份证中提取出生日期/性别/年龄/生肖/星座/省份/城市/城市等级/区县(2019划分标准)相关推荐

  1. 利用python进行身份证号码大全_2分钟就能学会的3个函数,快速从身份证中提取出生日期、年龄...

    做人力资源的小伙伴,经常要录入员工信息.今天考呀呀会计教育和大家分享,如何在员工身份证中,快速提取出生日期,自动生成年龄. 出生日期提取 在C2单元格输入公式=Mid(B2,7,8). Mid函数:用 ...

  2. access查询出生日期格式转换_从身份证中提取出生日期的3个方法和计算年龄和星座的方法...

    在我们日常的工作当中,经常会遇到通过身份证来获取出生年月日的需求,今天就给大家介绍三种可以从身份证中提取出生年月日的方法. 我们都知道身份证不同的区域是有不同的含义的,代表出生年月日的数字是第7位到第 ...

  3. 用python从身份证中提取生日信息(切片计算)

    ''' 功能:从身份证号码获取生日 作者:文雅兰 日期:2021年11月25日 '''import re regx = '^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0 ...

  4. 通过身份证号提取性别_身份证号提取生日、年龄、性别、籍贯,最简单的方法!...

    上一期,Amy跟大家分享了如何从身份证号中提取出生日期.今天我们继续来讲如何提取年龄.性别.籍贯. 身份证号提取年龄 ✦思路:在E2单元格输入公式"=DATEDIF(D2,TODAY(),& ...

  5. 如何用oracle从身份证信息中提取出生日期?

    在项目开发中,有从身份证信息中提取出生日期的需求: 1.对18位身份证,因为有多个表,所以用到它们的关联,示例如下: update pfapperosoninformation  p set p.pb ...

  6. sql server 根据身份证号计算出生日期和年龄的存储过程

    我这边有一个业务,需要客户填写身份证号,自动计算他的出生日期和年龄 在sql中,具体的存储过程实现是这样的: /******************************************** ...

  7. python中需要用到的英文词汇-使用python从语料库中提取最常用的单词

    也许这是一个愚蠢的问题,但是我在使用Python从语料库中提取十个最常见的单词时遇到了问题.这就是到目前为止. (顺便说一句,我与NLTK一起阅读一个带有两个子类别的语料库,每个子类别有10个.txt ...

  8. python 从字符串中提取数字 re.findall()

    以前老用(.*?)提取数字,今天发现不对了,比如一行数字为: 0 0.248438 0.255556 0.128125 0.194444 用: re.findall('(.*?) (.*?) (.*? ...

  9. 使用OpenCV和Python从图像中提取形状

    Welcome to the first post in this series of blogs on extracting features from images using OpenCV an ...

最新文章

  1. 【整理】NSTimer使用及注意事项
  2. 鸿蒙系统手机mate40,鸿蒙系统来了!华为Mate40首批,2年前手机将被淘汰
  3. 自定义Lisp透明命令
  4. scrum敏捷开发工具leangoo如何添加成员
  5. angularjs 结构的两种写法(2)
  6. 用c语言单链表编写贪吃蛇程序6,C语言链表实现贪吃蛇游戏
  7. Android IPC 结篇
  8. [转载] 交互式数据可视化在Python中用Bokeh实现
  9. 2020 CPU性能排行天梯图
  10. 开发手机APP的一些心得体会
  11. 8、鼠标控制与32位模式切换
  12. ONF执行主席Dan Pitt:2014至2016 SDN 的市场化之路
  13. PCL——超体素(SuperVoxel)、超体聚类分割
  14. c3p0存在严重bug
  15. vue中使用AES加密与AES解密
  16. Oracle 12c PDB浅析(r9笔记第10天)
  17. 我最喜爱的职业是计算机300字,我喜爱的职业作文(精选6篇)
  18. 《网管员必读——网络应用》(第2版)试读样章下载
  19. 计算机管理要继续请输入,win7安装魔法优化大师提示“若要继续,请键入管理员密码”怎么办...
  20. Cocos2d-JS实现的贪吃蛇

热门文章

  1. ubuntu 配置 riscv rocket-chip
  2. 鸿蒙系统下的搜狗输入法,mac搜狗输入法-搜狗输入法Mac版下载 V6.0.3.12502-PC6苹果网...
  3. 面试官:百万数据的导入导出解决方案,怎么设计?
  4. Java计算机毕业设计视频点播系统演示录像源码+系统+数据库+lw文档
  5. Java循环嵌套小游戏
  6. 哈佛大学 47 清华大学 中国 1911年
  7. Linux关机各种关机命令总结
  8. 红米4鸿蒙系统刷机包,红米4官方固件rom系统线刷刷机包_红米4线刷官方系统升级包...
  9. 项目过程管理(十六)项目周报
  10. SketchUp:SketchUp草图大师软件使用技巧总结(视图操作,绘图环境优化,缩放/镜像/对齐/桌边倒圆角案例)