Python實現(xiàn)隨機生成有效手機號碼及身份證功能示例
本文實例講述了Python實現(xiàn)隨機生成有效手機號碼及身份證功能。分享給大家供大家參考,具體如下:
中國那么大,人那么多,幾乎人手一部手機。手機號碼已經(jīng)作為各大互聯(lián)網(wǎng)站的注冊賬戶。同樣,身份證更是如此。以下是生成有效手機號碼和身份證號。
身份證需要下載districtcode.txt文件。
完整代碼如下:
import os import random import datetime BASE_DIR = os.path.dirname(os.path.dirname(__file__)) DC_PATH = BASE_DIR + "districtcode.txt" # 隨機生成手機號碼 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)) # 隨機生成身份證號 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ū)項 id = id + str(random.randint(1930,2013)) #年份項 da = datetime.date.today()+datetime.timedelta(days=random.randint(1,366)) #月份和日期項 id = id + da.strftime('%m%d') id = id+ str(random.randint(100,300))#,順序號簡單處理 i = 0 count = 0 weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] #權(quán)重項 checkcode ={'0':'1','1':'0','2':'X','3':'9','4':'8','5':'7','6':'6','7':'5','8':'5','9':'3','10':'2'} #校驗碼映射 for i in range(0,len(id)): count = count +int(id[i])*weight[i] id = id + checkcode[str(count%11)] #算出校驗碼 return id print createPhone() print gennerator()
運行結(jié)果如下:
PS:這里再提供一款本站身份證歸屬地信息查詢工具供大家參考:
身份證歸屬地信息在線查詢:
http://tools.jb51.net/bianmin/sfz
另外,本站在線工具小程序上也有一款功能更加強大的身份證信息獲取工具,感興趣的朋友可以掃描如下小程序碼查看:
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
詳解python數(shù)據(jù)結(jié)構(gòu)和算法
這篇文章主要介紹了python數(shù)據(jù)結(jié)構(gòu)和算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04jupyter?notebook?自定義python解釋器的過程詳解
大家都知道jupyter?notebook?網(wǎng)頁版交互環(huán)境,類似于ipython,功能強大,這篇文章主要介紹了jupyter?notebook?自定義python解釋器的過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10