通过制作一个身份证解析案例来学习字符串的常用操作
创建一个类及需要初始化的信息
- 先创建一个类的属性
ID_number
通过使用input来动态获取输入后的一个身份证信息 - 根据类属性
ID_number
通过index定位对应需要的value作为__init__(self):初始值(后续会用到)
class ShenFenZheng(object):
ID_number = input('请输入您的身份证号码')
@staticmethod
def ps():
print('欢迎来到解析身份证信息软件')
def __init__(self):
self.dict_data = {'41': '河南省', '42': '湖北省', '43': '湖南省', '44': '广东省', '45': '广西省', '46': '海南省', '50': '重庆市',
'51': '四川省',
'52': '贵州省', '53': '云南省', '54': '西藏省', '61': '陕西省', '62': '甘肃省', '63': '青海省', '64': '宁夏省',
'65': '新疆省',
'11': '北京市', '12': '天津市', '13': '河北省', '14': '山西省', '15': '内蒙古', '21': '辽宁省', '22': '吉林省',
'23': '黑龙江', '31': '上海市', '32': '江苏省', '33': '浙江省', '34': '安徽省', '35': '福建省', '36': '江西省', '37': '山东省'}
try:
self.year = ShenFenZheng.ID_number[6:10]
self.month = ShenFenZheng.ID_number[10:12]
self.day = ShenFenZheng.ID_number[12:14]
self.gender = int(ShenFenZheng.ID_number[16])
self.province = ShenFenZheng.ID_number[0:2]
except IndexError:
print('输入的信息格式有问题')
注:
@staticmethod 是面向对象类中的静态方法
逻辑思路分析
- 先判断输入的信息是否是18位的数字类型,如包含了空格、输入的信息包含非数字,以及数字未满18位都是不规范需要重新输入
def page(self):
try:
if ShenFenZheng.ID_number.isspace():
print('您输入的信息包含了空格')
elif not ShenFenZheng.ID_number.isdigit():
print('输入的非数字请重新输入')
elif ShenFenZheng.ID_number.isdigit():
if len(ShenFenZheng.ID_number) is 18:
print(f'输入正确,您的身份证是:{ShenFenZheng.ID_number}')
print(f'您的生日是:{self.year}年{self.month}月{self.day}日')
print(f'省份:{self.dict_data[self.province]}')
- 根据身份证倒数第2位判断偶数位女性、反之男性判断
if self.gender % 2 == 0:
注:性别判断内的if嵌套是为了根据身份证中的XX月XX日来判断星座
if self.gender % 2 == 0:
print('性别:女')
if ('03' == self.month and '21' <= self.day) or ('04' == self.month and self.day <= '19'):
constellation = '白羊座'
elif ('04' == self.month and '20' <= self.day) or ('05' == self.month and self.day <= '20'):
constellation = '金牛座'
elif ('05' == self.month and '21' <= self.day) or ('06' == self.month and self.day <= '21'):
constellation = '双子座'
elif ('06' == self.month and '22' <= self.day) or ('07' == self.month and self.day <= '22'):
constellation = '巨蟹座'
elif ('07' == self.month and '23' <= self.day) or ('08' == self.month and self.day <= '22'):
constellation = '狮子座'
elif ('08' == self.month and '23' <= self.day) or ('09' == self.month and self.day <= '22'):
constellation = '处女座'
elif ('09' == self.month and '23' <= self.day) or ('10' == self.month and self.day <= '23'):
constellation = '天秤座'
elif ('10' == self.month and '24' <= self.day) or ('11' == self.month and self.day <= '22'):
constellation = '天蝎座'
elif ('11' == self.month and '23' <= self.day) or ('12' == self.month and self.day <= '21'):
constellation = '射手座'
elif ('12' == self.month and '22' <= self.day) or ('01' == self.month and self.day <= '19'):
constellation = '摩羯座'
elif ('01' == self.month and '20' <= self.day) or ('02' == self.month and self.day <= '18'):
constellation = '水瓶座'
elif ('02' == self.month and '19' <= self.day) or ('03' == self.month and self.day <= '20'):
constellation = '双鱼座'
else:
constellation = '未匹配的星座'
print(f'星座:{constellation}')
else:
print('性别:男')
if ('03' == self.month and '21' <= self.day) or ('04' == self.month and self.day <= '19'):
constellation = '白羊座'
elif ('04' == self.month and '20' <= self.day) or ('05' == self.month and self.day <= '20'):
constellation = '金牛座'
elif ('05' == self.month and '21' <= self.day) or ('06' == self.month and self.day <= '21'):
constellation = '双子座'
elif ('06' == self.month and '22' <= self.day) or ('07' == self.month and self.day <= '22'):
constellation = '巨蟹座'
elif ('07' == self.month and '23' <= self.day) or ('08' == self.month and self.day <= '22'):
constellation = '狮子座'
elif ('08' == self.month and '23' <= self.day) or ('09' == self.month and self.day <= '22'):
constellation = '处女座'
elif ('09' == self.month and '23' <= self.day) or ('10' == self.month and self.day <= '23'):
constellation = '天秤座'
elif ('10' == self.month and '24' <= self.day) or ('11' == self.month and self.day <= '22'):
constellation = '天蝎座'
elif ('11' == self.month and '23' <= self.day) or ('12' == self.month and self.day <= '21'):
constellation = '射手座'
elif ('12' == self.month and '22' <= self.day) or ('01' == self.month and self.day <= '19'):
constellation = '摩羯座'
elif ('01' == self.month and '20' <= self.day) or ('02' == self.month and self.day <= '18'):
constellation = '水瓶座'
elif ('02' == self.month and '19' <= self.day) or ('03' == self.month and self.day <= '20'):
constellation = '双鱼座'
else:
constellation = '未匹配的星座'
print(f'星座:{constellation}')
完整代码
class ShenFenZheng(object):
ID_number = input('请输入您的身份证号码')
@staticmethod
def ps():
print('欢迎来到解析身份证信息软件')
def __init__(self):
self.dict_data = {'41': '河南省', '42': '湖北省', '43': '湖南省', '44': '广东省', '45': '广西省', '46': '海南省', '50': '重庆市',
'51': '四川省',
'52': '贵州省', '53': '云南省', '54': '西藏省', '61': '陕西省', '62': '甘肃省', '63': '青海省', '64': '宁夏省',
'65': '新疆省',
'11': '北京市', '12': '天津市', '13': '河北省', '14': '山西省', '15': '内蒙古', '21': '辽宁省', '22': '吉林省',
'23': '黑龙江', '31': '上海市', '32': '江苏省', '33': '浙江省', '34': '安徽省', '35': '福建省', '36': '江西省', '37': '山东省'}
try:
self.year = ShenFenZheng.ID_number[6:10]
self.month = ShenFenZheng.ID_number[10:12]
self.day = ShenFenZheng.ID_number[12:14]
self.gender = int(ShenFenZheng.ID_number[16])
self.province = ShenFenZheng.ID_number[0:2]
except IndexError:
print('输入的信息格式有问题')
def page(self):
try:
if ShenFenZheng.ID_number.isspace():
print('您输入的信息包含了空格')
elif not ShenFenZheng.ID_number.isdigit():
print('输入的非数字请重新输入')
elif ShenFenZheng.ID_number.isdigit():
if len(ShenFenZheng.ID_number) is 18:
print(f'输入正确,您的身份证是:{ShenFenZheng.ID_number}')
print(f'您的生日是:{self.year}年{self.month}月{self.day}日')
print(f'省份:{self.dict_data[self.province]}')
if self.gender % 2 == 0:
print('性别:女')
if ('03' == self.month and '21' <= self.day) or ('04' == self.month and self.day <= '19'):
constellation = '白羊座'
elif ('04' == self.month and '20' <= self.day) or ('05' == self.month and self.day <= '20'):
constellation = '金牛座'
elif ('05' == self.month and '21' <= self.day) or ('06' == self.month and self.day <= '21'):
constellation = '双子座'
elif ('06' == self.month and '22' <= self.day) or ('07' == self.month and self.day <= '22'):
constellation = '巨蟹座'
elif ('07' == self.month and '23' <= self.day) or ('08' == self.month and self.day <= '22'):
constellation = '狮子座'
elif ('08' == self.month and '23' <= self.day) or ('09' == self.month and self.day <= '22'):
constellation = '处女座'
elif ('09' == self.month and '23' <= self.day) or ('10' == self.month and self.day <= '23'):
constellation = '天秤座'
elif ('10' == self.month and '24' <= self.day) or ('11' == self.month and self.day <= '22'):
constellation = '天蝎座'
elif ('11' == self.month and '23' <= self.day) or ('12' == self.month and self.day <= '21'):
constellation = '射手座'
elif ('12' == self.month and '22' <= self.day) or ('01' == self.month and self.day <= '19'):
constellation = '摩羯座'
elif ('01' == self.month and '20' <= self.day) or ('02' == self.month and self.day <= '18'):
constellation = '水瓶座'
elif ('02' == self.month and '19' <= self.day) or ('03' == self.month and self.day <= '20'):
constellation = '双鱼座'
else:
constellation = '未匹配的星座'
print(f'星座:{constellation}')
else:
print('性别:男')
if ('03' == self.month and '21' <= self.day) or ('04' == self.month and self.day <= '19'):
constellation = '白羊座'
elif ('04' == self.month and '20' <= self.day) or ('05' == self.month and self.day <= '20'):
constellation = '金牛座'
elif ('05' == self.month and '21' <= self.day) or ('06' == self.month and self.day <= '21'):
constellation = '双子座'
elif ('06' == self.month and '22' <= self.day) or ('07' == self.month and self.day <= '22'):
constellation = '巨蟹座'
elif ('07' == self.month and '23' <= self.day) or ('08' == self.month and self.day <= '22'):
constellation = '狮子座'
elif ('08' == self.month and '23' <= self.day) or ('09' == self.month and self.day <= '22'):
constellation = '处女座'
elif ('09' == self.month and '23' <= self.day) or ('10' == self.month and self.day <= '23'):
constellation = '天秤座'
elif ('10' == self.month and '24' <= self.day) or ('11' == self.month and self.day <= '22'):
constellation = '天蝎座'
elif ('11' == self.month and '23' <= self.day) or ('12' == self.month and self.day <= '21'):
constellation = '射手座'
elif ('12' == self.month and '22' <= self.day) or ('01' == self.month and self.day <= '19'):
constellation = '摩羯座'
elif ('01' == self.month and '20' <= self.day) or ('02' == self.month and self.day <= '18'):
constellation = '水瓶座'
elif ('02' == self.month and '19' <= self.day) or ('03' == self.month and self.day <= '20'):
constellation = '双鱼座'
else:
constellation = '未匹配的星座'
print(f'星座:{constellation}')
elif not len(ShenFenZheng.ID_number) is 18:
print('您输入的身份证号不正确少于18位')
except ValueError:
print('你输入的非中文字符请重新输入')
def run(self):
while True:
select = int(input('是否继续使用,1、继续 2、退出'))
if select == 1:
self.page()
else:
print('欢迎使用,期待您的下次来临!')
break
if __name__ == '__main__':
ShenFenZheng.ps()
t1 = ShenFenZheng()
t1.run()
以上代码纯属练习娱乐,觉得还不错请给一个赞谢谢各位观看者,你们的赞就是文章发布的动力感谢各位。
最后来一个结果图: