根据身份证号获取生日、生肖、星座、性别。
def user_info(id_card):
# 根据身份证号获取一些基本信息
year = id_card[6:10]
month = id_card[10:12]
day = id_card[12:14]
sex = '男' if int(id_card[-2]) % 2 == 1 else '女'
birthday = '{0}-{1}-{2}'.format(year, month, day)
zodiac = '猴鸡狗猪鼠牛虎兔龙蛇马羊'[int(year) % 12]
constellation_list = ('摩羯座', '水瓶座', '双鱼座', '白羊座', '金牛座', '双子座',
'巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座')
constellation_day = (
(1, 20), (2, 19), (3, 21), (4, 21), (5, 21), (6, 22), (7, 23), (8, 23), (9, 23), (10, 23), (11, 23),
(12, 23))
constellation = constellation_list[
len(list(filter(lambda y: y <= (int(month), int(day)), constellation_day))) % 12]
data = {
'birthday': birthday,
'zodiac': zodiac,
'constellation': constellation,
'sex': sex
}
return data
校验身份证
def verity_id_card(id_card):
# 身份证校验
verity = dict(zip([x for x in range(0, 11)], ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2', '1']))
sum = 0
for x, y in enumerate(id_card[:-1]):
sum += ((2 ** (18 - x - 1)) % 11) * int(y) # 17位对应系数相乘的和
if id_card[-1] == verity[sum % 11]: # 校验码对照
return True
else:
return False