Python Basics Tutorial: Using Python to complete 15 of 18 ID card system conversion function

This article describes the use of Python complete 15 of 18 ID card system conversion function, this paper to introduce a very detailed, with some reference value for reference, need friends can refer to the Here Insert Picture Description
recent work just to clean a number of customers data related to the ID number 15 and 18-bit converter, specially studied under the shared here.

ID numbers forming

Since talking about the ID card conversion, it would need to understand the composition under document number. Here Insert Picture Description
Citizenship is characterized by a combination code number, between 17-digit code and a body composed of a single digit check code;

The order from left to right: six digit code address, date of birth eight-digit code, a three digit numerical sequence code and check code.

Six-digit address code: an encoding target permanent residence county (city, banner, district) administrative division code, according to the provisions GB / T 2260's.

Eight-digit code Date of birth: an encoding target birth year, month, day, according to the provisions GB / T 7408's. Years, without separators between month, date code. Date of birth as someone August 12, 1995, its date of birth code is 19950812.

Three order code: representation in the regional context of the same address code identified, for the same year, the same month, people born the same day of the scheduled sequence number, odd-order code assigned to men, women assigned to the even number.

A check code: Check code in accordance with ISO 7064: calculated checksum check code 1983.MOD 11-2.

Checksum calculation method

1, the ID number 17 in front of the median multiplied by different coefficients. From first to seventeenth coefficients are: 7,910,584,216,379,105,842;

2, the results of these 17 digital multiplying and adding coefficients;

3, by adding up and divided by 11, the remainder is to see how much;

4, the remainder may have only 012,345,678,910 these 11 digits. Which are mapped to the last bit of the identification number is 1 0 X 9 8 7 6 5 4 3 2;

5, by the above that if the remainder is 2, Roman numeral X. will appear in the first 18 digital ID

Solutions

15 rpm 18: i.e., the first six digits of the identification number ID sixth ++ '19' after the numeric check code +

(Do not ask me why this idiot plus 19 ('⊙ω⊙`) @? ¥ &? The problem, of course, because only 19 century people may have 15 friends ID number)

Checksum calculation method even simpler, these numbers and the results of 17 divided by the sum of the coefficients multiplying the number to 11 to match the number corresponding to the remainder.

Note: The code I used several variables, here at dismantling explain.

AI: indicates the ID number on the i-th digital position value Wi: Wi denotes a weight factor on the i-th position: 7,910,584,216,379,105,842

Body digit code seventeen weighted sum equation: S = Sum (AiWi), i = 0, ..., 16, the right front summing the first digit 17

Calculating modulus Y = mod (S, 11)

Obtained by molding the corresponding check code

Y: 0 1 2 3 4 5 6 7 8 9 10

Check code: 1 0 X 9 8 7 6 5 4 3 2

OK, analysis of almost a direct look at the code.

How to use code to achieve?

# encoding: utf-8
"""
CREATED ON 19-11-05
@AUTHOR: XUSL
"""
WI = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1, ]
VI = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2, ]
DEF GET_VERITY(EIGHTEEN_CARD):
  """
  :PARAM EIGHTEEN_CARD:
  :RETURN:
  """
  AI = []
  REMAINING = ''
  IF LEN(EIGHTEEN_CARD) == 18:
    EIGHTEEN_CARD = EIGHTEEN_CARD[0:-1]
  IF LEN(EIGHTEEN_CARD) == 17:
    S = 0
    FOR I IN EIGHTEEN_CARD:
      AI.APPEND(INT(I))
    FOR I IN RANGE(17):
      S = S + WI[I] * AI[I]
    REMAINING = S % 11
  RETURN 'X' IF REMAINING == 2 ELSE STR(VI[REMAINING])
DEF UP_TO_EIGHTEEN(FIFTEEN_CARD):
  """
  15位转18位
  :PARAM FIFTEEN_CARD:
  :RETURN:
  """
  EIGHTEEN_CARD = FIFTEEN_CARD[0:6] + '19' + FIFTEEN_CARD[6:15]
  RETURN EIGHTEEN_CARD + GET_VERITY(EIGHTEEN_CARD)
DEF DOWN_TO_FIFTEEN(EIGHTEEN_CARD):
  """
  18位转15位
  :PARAM EIGHTEEN_CARD:
  :RETURN:
  """
  RETURN EIGHTEEN_CARD[0:6] + EIGHTEEN_CARD[8:17]
IF __NAME__ == '__MAIN__':
  # 15位转18位
  CARD_1 = UP_TO_EIGHTEEN('632123820927051')
  PRINT(CARD_1)
  # 18位转15位
  CARD_2 = DOWN_TO_FIFTEEN('410125199908222000')
  PRINT(CARD_2)

Of course, this is just a small function, mainly want to share under the code, if there is the same process can be used directly.

Finally, we recommend a very wide python learning resource gathering, [click to enter] , here are my collection before learning experience, learning pen

Remember, there is a glimmer of corporate experience, and calmed down to zero on the basis of the actual project data, we can also below the message, not the

Understand proposed, we will study together progress

to sum up

The above is a small series to introduce the use of Python complete system conversion function 15 18 ID cards, we hope to help

Published 57 original articles · won praise 25 · views 70000 +

Guess you like

Origin blog.csdn.net/haoxun11/article/details/105129095