python生成身份证号码第18位的校验码

国内身份证号码是18位的, 以前是15位的. 

123456yyyymmdd001Z

123456 是你的户籍所在地编码

yyyymmdd是你的出生日期

001是你这一天出生的人的编号, 或者其他的什么编号

Z是校验码, 可能会出现x, 这是因为校验码的算法决定的. 因为它用11除的. 余数决定的


我在百度上发现了一个c语言的算法.

然后, 我把它给改成了python版本的, 需要的直接复制吧, 不需要谢我.


#!/usr/bin/env python
__author__ = '糊糊, <[email protected]>'
def jiaoyanma(shenfenzheng17):
    def haoma_validate(shenfenzheng17):
        if type(shenfenzheng17) in [str,list,tuple]:
            if len(shenfenzheng17) == 17:
                return True
        raise Exception('Wrong argument')
    
    if haoma_validate(shenfenzheng17):
        if type(shenfenzheng17) == str:
            seq = map(int,shenfenzheng17)
        elif type(shenfenzheng17) in [list,tuple]:
            seq = shenfenzheng17
        
        t = [7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
        s = sum(map(lambda x:x[0]*x[1],zip(t,map(int,seq))))
        b = s % 11
        bd={
            0: '1',
            1: '0',
            2: 'x',
            3: '9',
            4: '8',
            5: '7',
            6: '6',
            7: '5',
            8: '4',
            9: '3',
           10: '2'
        }
        print shenfenzheng17
        return bd[b]
     
if __name__ == '__main__':
    print jiaoyanma('22132119880830001')



你可能感兴趣的:(算法,python,身份证号码,校验码)