# 本代码需配合文件 RegionCode 使用(索引地区)
# 中国行政区划代码已事先从中国民政部爬取。


class IdCardAnalyse(object):
    def __init__(self):
        self.name = input("请输入您的姓名:")
        self.id = input("请输入您的身份证号码:")
        self.region_code = eval(open("RegionCode", "r", encoding="utf-8").read())

    def check(self):
        ai = list(self.id[:17])
        wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        result_sum = sum([int(ai[index]) * wi[index] for index in range(len(ai))])
        
        result = (12 - (result_sum % 11)) % 11
        if result == 10:
            result = "X"
        
        if str(self.id[-1]).upper() == result or int(self.id[-1]) == result:
            return True
        else:
            return False


    def work(self):
        if self.check():
            region = self.region_code[self.id[:6]]
            birth = self.id[6:10] + "-" + self.id[10:12] + "-" + self.id[12:14]

            if (int(self.id[16]) % 2) == 0:
                sex = "女"
            else:
                sex = "男"
            print("姓名:%s||性别:%s||地区:%s||出生:%s" % (self.name, sex, region, birth))

        else:
            print("身份证号码有误,请检查身份证信息!")


if __name__ == "__main__":
    i = IdCardAnalyse()
    i.work()
Last modification:April 14, 2020
如果觉得我的文章对你有用,请随意赞赏