中国那么大,人那么多,几乎人手一部手机。手机号码已经作为各大互联网站的注册账户。同样,身份证更是如此。以下是生成有效手机号码和身份证号。
  身份证需要下载districtcode.txt这个文件:http://files.cnblogs.com/files/yicaifeitian/districtcode.rar

 BASE_DIR = os.path.dirname(os.path.dirname(__file__))
 DC_PATH = BASE_DIR  + "districtcode.txt"

 # 随机生成手机号码
 def createPhone():
     prelist=["]
     ") 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 = date.today()+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 ={'} #校验码映射
     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()

  

Python 随机生成有效手机号码及身份证的更多相关文章

  1. python随机生成个人信息

    python随机生成个人信息 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import sys import random class Personal ...

  2. Python随机生成验证码的两种方法

    Python随机生成验证码的方法有很多,今天给大家列举两种,大家也可以在这个基础上进行改造,设计出适合自己的验证码方法方法一:利用range Python随机生成验证码的方法有很多,今天给大家列举两种 ...

  3. 用python随机生成数据,再插入到postgresql中

    用python随机生成学生姓名,三科成绩和班级数据,再插入到postgresql中. 模块用psycopg2 random import random import psycopg2 fname=[' ...

  4. python随机生成手机号码

    一句话生成电话号码random.choice(['139','188','185','136','158','151'])+"".join(random.choice(" ...

  5. python 随机生成固定长度的字串

    from random import Random#随机生成4到20位的用户名def random_username(): username = '' chars = 'AaBbCcDdEeFfGgH ...

  6. python随机生成6位数验证码

    #随机生成6位数验证码 import randomcode = []for i in range(6):    if i == str(random.randint(1,5)):        cod ...

  7. python随机生成库faker库api详解

    # -*- coding: utf-8 -*- # @Author : FELIX # @Date : 2018/6/30 9:49 from faker import Factory # zh_CN ...

  8. python随机生成字符

    Python2: Unicode是一种通用的编码方式,不论是英文字母.汉字.日语还是其他文字都能够对应一个唯一的Unicode编码(序号). chr(100) # 得到整数对应的ascii码(小于25 ...

  9. python 随机生成用户名、密码、手机号码

    #!C:\Python #!/usr/bin/env python #-*- coding:utf-8 -*- import string import random minlength = 6 ma ...

随机推荐

  1. php 1到100累加 新方法

    <?php $sum = 0; for($i=0;$i<=100;$i++){ $sum += $i; } echo $sum; 之前只是这么写. 现在发现可以这么写 $sum = arr ...

  2. 解决EditText不能撑满全屏的问题及EditText你应该知道的属性

    一般我们要实现去下图一的效果很简单: 两个EditText就搞定 效果图一:   但是我们想让第二个EditText撑满剩余空间怎么做?如效果图二 效果图二: 解决: 使用了ScrollView嵌套L ...

  3. 【mysql】关于InnoDB表text blob大字段的优化

    最近在数据库优化的时候,看到一些表在设计上使用了text或者blob的字段,单表的存储空间已经达到了近100G,这种情况再去改变和优化就非常难了 一.简介 为了清楚大字段对性能的影响,我们必须要知道i ...

  4. tmux鼠标配置出现错误unknown option: mode-mouse

    setw -g mode-mouse on set -g mouse-select-pane on set -g mouse-resize-pane on set -g mouse-select-wi ...

  5. juery悬浮框

    现在的淘宝啊,京东啊等很多平台都用到了一个技术,就是当页面下拉时,某个div会一直悬浮在页面顶端.具体代码如下<p>jQuery实现页面滚动时层智能浮动定位</p><!D ...

  6. 透视I/O多路复用

    透视I/O多路复用 我写的不是select这些函数的教学,需要了解的请自行Google或者去man,这些是帮助我理解函数的封装之下的道理. 需要回答的问题 I/O准备好了指什么?什么叫I/O已经可读/ ...

  7. 2017年4月 TIOBE 编程语言排名

    2017年4月 TIOBE 编程语言排名 Hack是Facebook 在三年推出的PHP方言,在2017年4月首次进入TIOBE编程语言排行榜前50位. Hack原是Facebook的内部项目,与20 ...

  8. 推送一个已有的代码到新的 gerrit 服务器

    1.指定项目代码库中迭代列出全部ProductList(.git)到pro.log文件中 repo forall -c 'echo $REPO_PROJECT' | tee pro.log pro.l ...

  9. stl_alloc.h分配器

    五.分配器:5.1.头文件: 5.1.1.include<stl_alloc.h> //内存的分配. 5.1.2.include<stl_construct.h> //对象的构 ...

  10. [Git]08 如何自动补全命令

     [Git]08如何自动补全命令 如果你用的是 Bash shell,可以试试看 Git 提供的自动完成脚本.下载 Git 的源代码,进入 contrib/completion 目录,会看到一个g ...