標籤:不能 while 使用者 its 拼接字串 not uppercase 判斷 lines
批量產生賬戶資訊,產生的賬戶由@sina.com結尾,長度由使用者輸入,產生多少條也由使用者輸入,使用者名稱不能重複,使用者名稱必須由大寫字母、小寫字母和數字組成。
1 def Users(num,len): # num產生多少條資訊,len帳號的長度 2 ‘‘‘ 3 用交集來判斷是否包含大小寫字母 4 :param num: 產生幾條資料 5 :param len: 帳號的長度 6 :return: 7 ‘‘‘ 8 results = [] # 存放結果的數組 9 uppers = set(string.ascii_uppercase) # 大寫字母10 lowers = set(string.ascii_lowercase) # 小寫字母11 digit = set(string.digits) # 數字12 all_str = string.ascii_letters + string.digits13 count = 014 while count < num:15 res = random.sample(all_str,len) # sample 返回的是一個list16 # 如果set列表中包含大小寫字母和數字17 if set(res) & uppers and set(res) & lowers and set(res) & digit:18 res = ‘‘ .join(res) + ‘@sina.com‘ + ‘\n‘ # 拼接字串19 if res not in results: # 如果res不在列表results中20 results.append(‘‘ .join(res)) # 將拼接的字串加到results列表中21 count += 122 with open(‘results.txt‘,‘w‘) as fw:23 fw.writelines(results) # 將列表寫入到檔案中24 25 Users(5,6) # 調用函數
results.txt 產生的資料
1 [email protected]2 [email protected]3 [email protected]4 [email protected]5 [email protected]
Python寫一個批量產生帳號的函數