1. 程式人生 > >用Python寫一個批量生成賬號的函數(用戶控制數據長度、數據條數)

用Python寫一個批量生成賬號的函數(用戶控制數據長度、數據條數)

shuf open 小寫 長度 數據 ase 函數 用戶控制 app

# 1、寫一個函數,批量生成一些註冊使用的賬號:[email protected],長度由用戶輸入,產生多少條也由用戶輸入,用戶名不能重復,用戶名必須由大寫字母、小寫字母、數字組成
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) + [email protected]
/* */ +‘\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) + [email protected]
/* */ + ‘\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寫一個批量生成賬號的函數(用戶控制數據長度、數據條數)