python寫的隨機生成身份證號碼的函式
阿新 • • 發佈:2019-02-16
import time, random
ARR = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
LAST = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
def makeNew():
u''' 隨機生成新的18為身份證號碼 '''
t = time.localtime()[0]
x ='%02d%02d%02d%04d%02d%02d%03d'%(random.randint(10,99),
random.randint( 01,99),
random.randint(01,99),
random.randint(t -80, t -18),
random.randint(1,12),
random.randint(1,28),
random.randint( 1,999))
y = 0
for i in range(17):
y += int(x[i]) * ARR[i]
return'%s%s'%(x, LAST[y %11])
def isTrue():
u''' 驗證身份證號碼是否真實號碼 '''
print u'請輸入身份證號碼'
x1 = raw_input('?')
xlen = len(x1)
if xlen !=18and xlen !=15:
return u'身份證號碼長度錯誤'
try:
if xlen ==18:
x2 = x1[6:14]
x3 = time.strptime(x2, '%Y%m%d')
if x2 <'19000101'or x3 > time.localtime():
return u'時間錯誤,超過允許的時間範圍'
else:
x2 = time.strptime(x1[6:12], '%y%m%d')
except:
return u'時間錯誤,非合法時間'
if xlen ==18:
y = 0
for i in range(17):
y += int(x1[i]) * ARR[i]
if LAST[y %11] != x1[-1].upper():
return u'驗證碼錯誤'
return u'YES'
def old2new():
u''' 15位身份證號碼轉換為18位身份證號碼 '''
print u'請輸入15位老身份證號碼'
x1 = raw_input('?')
if len(x1) !=15:
return u'身份證號碼輸入錯誤,身份證號碼長度不為15位'
oldcard ='%s19%s'%(x1[:6], x1[6:])
y = 0
for i in range(17):
y += int(oldcard[i]) * ARR[i]
return'%s%s'%(oldcard, LAST[y %11])
ARR = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
LAST = ('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2')
def makeNew():
u''' 隨機生成新的18為身份證號碼 '''
t = time.localtime()[0]
x ='%02d%02d%02d%04d%02d%02d%03d'%(random.randint(10,99),
random.randint(
random.randint(01,99),
random.randint(t -80, t -18),
random.randint(1,12),
random.randint(1,28),
random.randint(
y = 0
for i in range(17):
y += int(x[i]) * ARR[i]
return'%s%s'%(x, LAST[y %11])
def isTrue():
u''' 驗證身份證號碼是否真實號碼 '''
print u'請輸入身份證號碼'
x1 = raw_input('?')
xlen = len(x1)
if xlen !=18and xlen !=15:
return u'身份證號碼長度錯誤'
try:
if xlen ==18:
x2
x3 = time.strptime(x2, '%Y%m%d')
if x2 <'19000101'or x3 > time.localtime():
return u'時間錯誤,超過允許的時間範圍'
else:
x2 = time.strptime(x1[6:12], '%y%m%d')
except:
return u'時間錯誤,非合法時間'
if xlen ==18:
y = 0
for i in range(17):
y += int(x1[i]) * ARR[i]
if LAST[y %11] != x1[-1].upper():
return u'驗證碼錯誤'
return u'YES'
def old2new():
u''' 15位身份證號碼轉換為18位身份證號碼 '''
print u'請輸入15位老身份證號碼'
x1 = raw_input('?')
if len(x1) !=15:
return u'身份證號碼輸入錯誤,身份證號碼長度不為15位'
oldcard ='%s19%s'%(x1[:6], x1[6:])
y = 0
for i in range(17):
y += int(oldcard[i]) * ARR[i]
return'%s%s'%(oldcard, LAST[y %11])