Python generates a valid mobile phone number and ID card randomly

Source: Internet
Author: User

China is so big, so many people, almost a mobile phone. The mobile phone number has been registered as an account of the major Internet stations. Similarly, the identity card is more so. The following is a valid mobile phone number and a social security number.
Base_dir =Os.path.dirname (Os.path.dirname (__file__)) Dc_path= Base_dir +"Districtcode.txt"# Randomly generated phone number def createphone (): Prelist=[" the","131"," the","133","134","135","136","137","138","139","147"," Max","151"," the","153","155","156","157","158","159","186","187","188"]    returnRandom.choice (prelist) +"". Join (Random.choice ("0123456789") forIinchRange8) # Randomly generated ID number def getdistrictcode (): With open (Dc_path) asFile:data=file.read () districtlist= Data.split ('\ n')      forNodeinchdistrictlist: #print nodeifnode[Ten: One] !=' ': State= node[Ten:].strip ()ifnode[Ten: One]==' 'and node[ A: -]!=' ': City= node[ A:].strip ()ifnode[Ten: One] ==' 'and node[ A: -]==' ': District= node[ -:].strip () code= node[0:6] Codelist.append ({" State": State," City": City,"District":d Istrict,"Code": Code}) Def gennerator ():Globalcodelist codelist= []     ifNot Codelist:getdistrictcode () ID= Codelist[random.randint (0, Len (codelist)) ['Code'] #地区项 ID= ID + str (random.randint (1930, -) ) #年份项 da= Date.today () +timedelta (Days=random.randint (1,366)) #月份和日期项 ID= ID + da.strftime ('%m%d') ID= id+ Str (random.randint ( -, -) #, sequence number simple processing I=0Count=0Weight= [7,9,Ten,5,8,4,2,1,6,3,7,9,Ten,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','Ten':'2'} #校验码映射 forIinchRange0, Len (ID)): Count= Count +int(Id[i]) *Weight[i] ID= ID + checkcode[str (count% One)] #算出校验码returnidprint createphone () print gennerator ()
  ID need to download districtcode.txt this file. Http://files.cnblogs.com/files/yicaifeitian/districtcode.rar

  

Python generates a valid mobile phone number and ID card randomly

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.