Python randomly generated identification numbers and checking function

This article describes the Python randomly generated ID number and check function, mentioned in the text of the checksum calculation method for everyone, a friend in need can refer to
the identity of the composition of way

People's Republic of China National Standard GB 11643-1999 "citizen identity number" provides that: Citizenship is characterized by a combination code number, between 17-digit code and a check code body composition.

18 embodiment is a combination of numbers: Here Insert Picture Description
area code (6) Date of Birth code (8 bits) sequence code (2) Sex Code (1) check code (1)

• area code refers to the citizens permanent residence county (city, town, district) administrative division code 110102 is as Beijing - Xicheng District. But residents of Hong Kong, Macao and Taiwan regions identification number is accurate only to the provincial level.
• Birth date code representation of citizens born in the calendar (4), month (2), Japan (2).
• order code representation at the regional level in the same area code is identified, for the same year, the same month, people born the same day of the scheduled sequence number.
• Gender representation code odd men, women represent an even number.
• The last digit is a check code, here is ISO 7064: 1983, MOD 11-2 checksum system. Checksum is a number, but if the last check code using the system code calculation is "10", because of a predetermined ID number is 18 bits, places "X" instead of a check code "10."

Checksum calculation method

. • 1 The ID number is marked as right to left a_1, a_2, \ cdots, a_ {18}, a_1 is the check code;
. • 2 calculates a weight coefficient W_i = 2 ^ {i-1 } \ \ bmod \ {11}

So: Here Insert Picture Description
Using Python to obtain identity cards checksum:

def get_check_digit(id_number):
  """通过身份证号获取校验码"""
  check_sum = 0
  for i in range(0, 17):
    check_sum += ((1 << (17 - i)) % 11) * int(id_number[i])
  check_digit = (12 - (check_sum % 11)) % 11
  return check_digit if check_digit < 10 else 'X'

Randomly generated ID

From the above combination, we can draw the following code:

@classmethod
def generate_id(cls, sex=0):
  """
  随机生成身份证号,sex = 0表示女性,sex = 1表示男性
  """
  # 随机生成一个区域码(6位数)
  area_info = random.randint(0, len(addr))
  id_number = str(addr[area_info][0])
  # 限定出生日期范围(8位数)
  start, end = "1960-01-01", "2000-12-30"
  days = (datetime.datetime.strptime(end, "%Y-%m-%d") - datetime.datetime.strptime(start, "%Y-%m-%d")).days + 1
  birth_days = datetime.datetime.strftime(
    datetime.datetime.strptime(start, "%Y-%m-%d") + datetime.timedelta(random.randint(0, days)), "%Y%m%d"
  )
  id_number += str(birth_days)
  # 顺序码(2位数)
  id_number += str(random.randint(10, 99))
  # 性别码(1位数)
  id_number += str(random.randrange(sex, 10, step=2))
  # 校验码(1位数)
  return id_number + str(cls(id_number).get_check_digit())

The main function Tools

if __name__ == '__main__':
  random_sex = random.randint(0, 1) # 随机生成男(1)或女(0)
  print IdNumberUtil.generate_id(random_sex) # 随机生成身份证号
  print IdNumberUtil('410326199507103197').area_id # 地址编码:410326
  print IdNumberUtil('410326199507103197').get_area_name() # 地址:汝阳县
  print IdNumberUtil('410326199507103197').get_birthday() # 生日:1995-7-10
  print IdNumberUtil('410326199507103197').get_age() # 年龄:23(岁)
  print IdNumberUtil('410326199507103197').get_sex() # 性别:1(男)
  print IdNumberUtil('410326199507103197').get_check_digit() # 校验码:7
  print IdNumberUtil.verify_id('410326199507103198') # 检验身份证是否正确:False

to sum up

The above is a Python small series to introduce randomly generated ID number and check function, we want to help
you more than how multi-content, and finally to recommend a good reputation in the number of public institutions [programmers], where there are a lot of old-timers learning

Skills, learning experience, interview skills, workplace experience and other share, the more carefully prepared the zero-based introductory information, information on actual projects,

The method has timed programmer Python explain everyday technology, to share some of the learning and the need to pay attention to small details
Here Insert Picture Description

Published 58 original articles · won praise 11 · views 50000 +

Guess you like

Origin blog.csdn.net/chengxun02/article/details/105129014