本文實(shí)例講述了Python實(shí)現(xiàn)隨機(jī)生成有效手機(jī)號(hào)碼及身份證功能。分享給大家供大家參考,具體如下:
中國(guó)那么大,人那么多,幾乎人手一部手機(jī)。手機(jī)號(hào)碼已經(jīng)作為各大互聯(lián)網(wǎng)站的注冊(cè)賬戶。同樣,身份證更是如此。以下是生成有效手機(jī)號(hào)碼和身份證號(hào)。
身份證需要下載districtcode.txt文件。
完整代碼如下:
import osimport randomimport datetimeBASE_DIR = os.path.dirname(os.path.dirname(__file__))DC_PATH = BASE_DIR + "districtcode.txt"# 隨機(jī)生成手機(jī)號(hào)碼def createPhone(): prelist=["130","131","132","133","134","135","136","137","138","139","147","150","151","152","153","155","156","157","158","159","186","187","188"] return random.choice(prelist)+"".join(random.choice("0123456789") for i in range(8))# 隨機(jī)生成身份證號(hào)def getdistrictcode():  with open(DC_PATH) as file:   data = file.read()   districtlist = data.split('/n')  for node in districtlist:  #print node   if node[10:11] != ' ':    state = node[10:].strip()   if node[10:11]==' 'and node[12:13]!=' ':    city = node[12:].strip()   if node[10:11] == ' 'and node[12:13]==' ':    district = node[14:].strip()    code = node[0:6]    codelist.append({"state":state,"city":city,"district":district,"code":code})def gennerator():  global codelist  codelist = []  if not codelist:  getdistrictcode() id = codelist[random.randint(0,len(codelist))]['code'] #地區(qū)項(xiàng)  id = id + str(random.randint(1930,2013)) #年份項(xiàng)  da = datetime.date.today()+datetime.timedelta(days=random.randint(1,366)) #月份和日期項(xiàng)  id = id + da.strftime('%m%d')  id = id+ str(random.randint(100,300))#,順序號(hào)簡(jiǎn)單處理  i = 0 count = 0 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] #權(quán)重項(xiàng)  checkcode ={'0':'1','1':'0','2':'X','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','10':'2'} #校驗(yàn)碼映射  for i in range(0,len(id)):   count = count +int(id[i])*weight[i]   id = id + checkcode[str(count%11)] #算出校驗(yàn)碼   return idprint createPhone()print gennerator()運(yùn)行結(jié)果如下:

PS:這里再提供一款本站身份證歸屬地信息查詢工具供大家參考:
身份證歸屬地信息在線查詢:
http://tools.jb51.net/bianmin/sfz
另外,本站在線工具小程序上也有一款功能更加強(qiáng)大的身份證信息獲取工具,感興趣的朋友可以掃描如下小程序碼查看:

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選