国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現隨機生成有效手機號碼及身份證功能示例

2020-01-04 17:16:36
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現隨機生成有效手機號碼身份證功能。分享給大家供大家參考,具體如下:

中國那么大,人那么多,幾乎人手一部手機。手機號碼已經作為各大互聯網站的注冊賬戶。同樣,身份證更是如此。以下是生成有效手機號碼和身份證號。

身份證需要下載districtcode.txt文件。

完整代碼如下:

import osimport randomimport datetimeBASE_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'] #地區項   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] #權重項   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 idprint createPhone()print gennerator()

運行結果如下:

Python,隨機,生成,手機號碼,身份證

 

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 皋兰县| 文昌市| 榆社县| 玛纳斯县| 海门市| 岳西县| 安庆市| 霍邱县| 荥阳市| 福安市| 保山市| 彩票| 易门县| 安宁市| 九寨沟县| 青神县| 吉首市| 宁阳县| 威海市| 葵青区| 淅川县| 芷江| 沂南县| 马龙县| 黄陵县| 桐乡市| 十堰市| 金阳县| 新兴县| 嘉祥县| 北碚区| 垣曲县| 赤水市| 宜川县| 兖州市| 西藏| 泰兴市| 天等县| 巴里| 方正县| 隆安县|