随机生成四要素

时间:2024-03-05 10:15:08
  1 import random
  2 from datetime import date
  3 from datetime import timedelta
  4 
  5 
  6 class fourEl():
  7 
  8     def __init__(self,sex):
  9         self.sex = sex
 10 
 11     def create_phone(self):
 12         # 第二个数字
 13         second = [3, 4, 5, 7, 8][random.randint(0, 4)]
 14         # 第三个数字
 15         third = {3: random.randint(0, 9),
 16                  4: [5, 7, 9][random.randint(0, 2)],
 17                  5: [i for i in range(10) if i != 4][random.randint(0, 8)],
 18                  7: [i for i in range(10) if i not in [4, 9]][random.randint(0, 7)],
 19                  8: random.randint(0, 9), }[second]
 20         # 最后8位数字
 21         suffix = random.randint(9999999, 100000000)
 22         # 拼接手机号
 23         return "1{}{}{}".format(second, third, suffix)
 24 
 25     def getdistrictcode(self):
 26         codelist = []
 27         # 读取地区码
 28         file = open(\'/Users/zhaodi/PycharmProjects/python3/学习/自动化测试脚本/txt文档/身份证号地址对照表.txt\',
 29                     encoding=\'ISO-8859-1\')
 30         lines = file.readlines()
 31         # 逐行读取
 32         for line in lines:
 33             # 如果每行中去重后不为空,并且6位数字中最后两位不为00,则添加到列表里。(最后两位为00时为省份或地级市代码)
 34             if line.lstrip().rstrip().strip() != \'\' and (line.lstrip().rstrip().strip())[:6][-2:] != \'00\':
 35                 codelist.append(line[:6])
 36         return codelist
 37 
 38     def create_idcard(self):
 39         codelist = self.getdistrictcode()
 40         id = codelist[random.randint(0, len(codelist))]  # 地区项
 41         id = id + str(random.randint(1950, 1998))  # 年份项
 42         da = date.today() + timedelta(days=random.randint(1, 366))  # 月份和日期项
 43         id = id + da.strftime(\'%m%d\')
 44         easyCode = str(random.randint(100, 300))
 45 
 46         sex = self.sex
 47         if sex == \'\':
 48             if int(easyCode) % 2 == 1:
 49                 easyCode = easyCode
 50             else:
 51                 easyCode = int(easyCode) + 1
 52 
 53         if sex == \'\':
 54             if int(easyCode) % 2 == 0:
 55                 easyCode = easyCode
 56 
 57             else:
 58                 easyCode = int(easyCode) + 1
 59 
 60         id = id + str(easyCode)
 61 
 62         # id = id + str(random.randint(100, 300))  # 顺序号简单处理
 63 
 64         i = 0
 65         count = 0
 66         weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]  # 权重项
 67         checkcode = {\'0\': \'1\', \'1\': \'0\', \'2\': \'X\', \'3\': \'9\', \'4\': \'8\',
 68                      \'5\': \'7\', \'6\': \'6\', \'7\': \'5\', \'8\': \'5\', \'9\': \'3\', \'10\': \'2\'}  # 校验码映射
 69         for i in range(0, len(id)):
 70             count = count + int(id[i]) * weight[i]
 71         id = id + checkcode[str(count % 11)]  # 算出校验码
 72         return id
 73 
 74     def create_name(self):
 75         xing = [\'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 76                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 77                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 78                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 79                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 80                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 81                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\']
 82 
 83         ming = [\'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 84                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 85                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 86                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 87                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 88                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 89                 \'\', \'\', \'\', \'\', \'\', \'使\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 90                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'西\', \'\',
 91                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 92                 \'\', \'\', \'\', \'\', \'\', \'便\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 93                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 94                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 95                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 96                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 97                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 98                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
 99                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
100                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
101                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
102                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
103                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
104                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
105                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
106                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
107                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
108                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
109                 \'\', \'\', \'\', \'\', \'\', \'穿\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
110                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
111                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
112                 \'怀\', \'\', \'\', \'广\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
113                 \'\', \'\', \'\', \'\', \'\', \'线\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
114                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
115                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
116                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
117                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
118                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
119                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
120                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
121                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
122                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
123                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
124                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
125                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
126                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
127                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
128                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
129                 \'\', \'退\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
130                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
131                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
132                 \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\', \'\',
133                 \'\', \'\']
134 
135         for i in range(1):
136             x = random.randint(0, len(xing))
137             m1 = random.randint(0, len(ming))
138             m2 = random.randint(0, len(ming))
139             n = (self.sex + xing[x] + ming[m1] + ming[m2])
140         return n
141 
142     def create_bankAccount(self):
143         # 工行卡号开头
144         # prefix = "622202"
145         prefix = "621483"  # 招行卡
146         for i in range(10):
147             prefix = prefix + str(random.randint(0, 9))
148         # print(prefix)
149         return prefix
150 
151 
152 if __name__ == \'__main__\':
153     shul = 0
154 
155     while shul < 10:
156         shul = shul + 1
157         a = fourEl(\'\')
158         b = a.create_name()
159         c = a.create_phone()
160         d = a.create_idcard()
161         e = a.create_bankAccount()
162 
163         dateTest = \'{},{},{},{}\n\'.format(b,c,d,e)
164         print(dateTest)
165 
166         # file = open(\'/Users/zhaodi/PycharmProjects/python3/学习/自动化测试脚本/txt文档/date\',\'a\',encoding=\'utf-8\')
167         file = open(\'/Users/zhaodi/PycharmProjects/python3/学习/自动化测试脚本/txt文档/Test.csv\',\'a\',encoding=\'utf-8\')
168 
169         file.write(dateTest)
170 
171     file.close()
172     print(\'写入完成\')