码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-06-29 12:39:51      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:append   txt   数字   int   write   end   shuffle   lines   sample   

# 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‘ +‘\n‘
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‘ + ‘\n‘
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)

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

标签:append   txt   数字   int   write   end   shuffle   lines   sample   

原文地址:http://www.cnblogs.com/ermm/p/7093745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!