1 #!/usr/bin/python3 2 import random 3 4 # 从"身份证地址对照表.txt"读取地址码和对应的地址,保存为字典 5 def createIDaddress(file): 6 D = [] 7 for line in open(file): 8 if not line[5] == '0': 9 D.append(line[0:6]) 10 return (D) 11 12 # 随机生日码 13 def createBrithday(sYear=1979,eYear=2009): 14 year = random.randint(sYear,eYear) 15 month = random.randint(1,12) 16 day = random.randint(1,28) 17 berthday = str(year).zfill(4)+str(month).zfill(2)+str(day).zfill(2) 18 return (berthday) 19 20 # 随机顺序码 1:男 2:女 21 def createRandomCode(numMax = 999,sex = '女'): 22 code = random.randint(100,numMax) 23 if sex == '男': 24 if code % 2 == 1: 25 # print('性别:男,code{}'.format(code)) 26 return code 27 else: 28 # print('性别:男,code{}'.format(code+1)) 29 return code+1 30 elif sex == '女': 31 if code % 2 == 0: 32 # print('性别:女,code{}'.format(code)) 33 return code 34 else: 35 # print('性别:女,code{}'.format(code+1)) 36 return code + 1 37 else: 38 return "输入错误:(男:1 女:2)" 39 40 # 计算校验码 41 def checkCode(number): 42 S = 43 int(number[0]) * 7 + 44 int(number[1]) * 9 + 45 int(number[2]) * 10 + 46 int(number[3]) * 5 + 47 int(number[4]) * 8 + 48 int(number[5]) * 4 + 49 int(number[6]) * 2 + 50 int(number[7]) * 1 + 51 int(number[8]) * 6 + 52 int(number[9]) * 3 + 53 int(number[10]) * 7 + 54 int(number[11]) * 9 + 55 int(number[12]) * 10 + 56 int(number[13]) * 5 + 57 int(number[14]) * 8 + 58 int(number[15]) * 4 + 59 int(number[16]) * 2 60 mod = S % 11 61 mod_dist = {0:'1', 1:'0', 2:'X', 3:'9', 4:'8', 5:'7', 6:'6', 7:'5', 8:'4', 9:'3', 10:'2'} 62 checkMod = mod_dist[mod] 63 return checkMod 64 65 # 生成二代身份证 参数1:要生成的数量 参数2:男:1 女:2 66 def idNumber(shuliang = 1,sex = '女'): 67 filePath = '/Users/zhaodi/PycharmProjects/python3/学习/自动化测试脚本/txt文档/身份证号地址对照表.txt' 68 idAdd = createIDaddress(filePath) 69 70 L = [] #生成空列表,存放生成的号码 71 while shuliang > 0: 72 addCode = random.choice(idAdd) # 随机地址码 73 brithady = createBrithday(1949,2009) #随机生日 74 randomCode = createRandomCode(999,sex) #随机code码 75 number = str(addCode) + str(brithady) + str(randomCode) #17位数字 76 cCode = checkCode(number) #检查码 77 78 # 合成身份证号 79 id_Card = number + cCode 80 print("身份证号:{}".format(id_Card)) 81 L.append(id_Card) 82 shuliang = shuliang - 1 83 return L 84 85 idNumber(1,'女')