ID card number of a resident, which consists of a 17-digit digital ontology code and a digital verification code, according to the provisions on citizenship numbers in the National Standard of the People's Republic of China (GB 11643-1999. The six-digit address code, eight-digit birth date code, three-digit sequence code, and one-digit verification code are arranged from left to right.
I. ID card number Composition
1. Address Code (the first six digits of the ID card number)
Indicates the Administrative Code of the county (city, town, district) where the resident account of the encoding object is located. Code of 1-2 provinces, autonomous regions, and municipalities directly under the Central Government; Code of 3-4 prefecture-level cities, umeng and autonomous prefecture; Code of 5-6 counties, counties, and districts.
You can directly obtain the corresponding relationship of the Administrative Division code from the website of the National Bureau of Statistics.
Specific address: http://www.stats.gov.cn/tjbz/xzqhdm/
2. Date of Birth (seventh to 14th digits of ID card number)
Indicates the year, month, and day of the encoding object. The year is represented by four digits, and there is no separator between the year, month, and day. For example, 19810511 is used for representation.
3. sequence code (ID card number: 15th to 17)
The sequence number of the person born in the same year, month, or day within the region indicated by the address code. Among them, the 17th-digit odd number is given to men, and the even number is given to women.
4. Verification Code (last digit of the ID card number)
The verification code is calculated based on the first 17-digit code according to the ISO 7064: 1983.mod 11-2 verification code. The checksum of the ending number is calculated by the number preparation unit according to the unified formula. If a person's ending number is 0-9, NO x will appear, but if the ending number is 10, in this case, X is used instead.
Ii. ID card verification code calculation method
#-*- coding:UTF-8 -*-import refrom datetime import datetime __all__ = ["GeneratIdentityChecker"] class ExceptionIdentityChecker(Exception): pass def GeneratIdentityChecker(country = "China"): cls = globals().get(country+"IdentityChecker") if cls: return cls() else: raise ExceptionIdentityChecker(country+" identity‘s checker not found") class ChinaIdentityChecker(): def __init__(self): self.anWi = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2) self.cnAreaPart = 6 self.cnMod = 11 self.csYearPre = "19" self.csCheckCode = "10X98765432" self.cnMinArea = 150000 self.cnMaxArea = 700000 def check(self,code,**options): code = code.upper() if len(code) not in (15,18) or not re.match(r"^\d{15}$|^\d{17}[\dxX]$", code): return False area = int(code[0:self.cnAreaPart]) if not (self.cnMinArea <= area <= self.cnMaxArea): return False birthday = self.csYearPre+code[6:12] if len(code) == 15 else code[6:14] try: datetime.strptime(birthday, "%Y%M%d") except: return False if len(code) == 18: wi = self.anWi total = 0 for i in range(16,-1,-1): total += int(code[i])*wi[i] if not self.csCheckCode[total%self.cnMod] == code[17]: return False for i in options: try: checker = getattr(self,"_check_"+i) except: return False if not checker(code,options[i]): return False return True def _check_sex(self,code,sex): sex_flag = code[14] if len(code) == 15 else code[16] sex = sex.upper() if sex == "M": return int(sex_flag)%2 == 1 elif sex == "F": return int(sex_flag)%2 == 0 else: return False if __name__ == "__main__": gic = GeneratIdentityChecker() print gic.check("34052419800101001X")
About ID card numbers