用Python写一个批量生成账号的函数(用户控制数据长度、数据条数)

# 1、写一个函数,批量生成一些注册使用的账号:产生的账号是以@163.com结尾,长度由用户输入,产生多少条也由用户输入,用户名不能重复,用户名必须由大写字母、小写字母、数字组成
import random,string
def Users(num,len):
result = []
a = string.ascii_lowercase
b = string.ascii_uppercase
c = string.digits
d = string.ascii_letters
count = 0
while count < num:
if len > 2:
a1 = random.choice(a)
b1 = random.choice(b)
c1 = random.choice(c)
d1 = random.sample(d,len-3)
d1.append(a1)
d1.append(b1)
d1.append(c1)
random.shuffle(d1)
users = ''.join(d1) + '@163.com' +' '
if users not in result:
result.append(users)
count +=1
else:
print('请输入大于2的长度')
break
with open('users.txt','w') as fw:
fw.writelines(result)
Users(1,3)


#用集合的方式实现
def USERS(num,len):
result = []
all_str = string.ascii_letters + string.digits
upp_str = string.ascii_uppercase
low_str = string.ascii_lowercase
str = set(string.digits)
count = 0
while count < num:
if len > 2:
res = random.sample(all_str,len)
if set(res) & set(upp_str) and set(res) & set(low_str) and set(res) & str:
user = ''.join(res) + '@163.com' + ' '
if user not in result:
result.append(user)
count +=1
else:
print('请输入大于2的长度!')
break
with open('users.txt','w') as fw:
fw.writelines(result)
USERS(3,3)
原文地址:https://www.cnblogs.com/ermm/p/7093745.html