1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| import random import csv import json
special_char="!\"#$%&\'()*+,-./[\\]^_`{|}~?" char = "abcdefghijklmnopqrstuvwxyz"
def get_special_char(): return special_char[random.randint(0,25)]
def get_number(): return random.randint(0,9)
def get_char(): return char[random.randint(0,25)]
def generate_code(num): x,y = -1,-1 while x==y: x = random.randint(0,num-1) y = random.randint(0,num-1) pwd = "" for i in range(num): if i == x: pwd += get_special_char() elif i == y: pwd += str(get_number()) else: pwd += get_char()
return pwd
if __name__ == '__main__': password_file = 'pwd.txt' stu_pwd_dict = {} files = open(password_file, 'r') stu_list = files.read().splitlines() while True: pattern = str(input("请输入您需要设定的位数模式,整体设置扣1,文件设置扣2")) if pattern=="1": while True: try: num = eval(input("请输入您需要设置的密码位数,支持6-18位:")) if num in range(6,19): print("密码生成中,请稍候……") for each in stu_list: pwd = generate_code(num) stu_pwd_dict[each] = pwd break else: print("您输入的密码位数有误,请重新输入:") except: print("您输入的数字格式有误,请重新输入:") break elif pattern=="2": for each in stu_list: id,num = each.split("_") pwd = generate_code(int(num)) stu_pwd_dict[id] = pwd break else: print("您输入的模式代码有误,请重新输入:") continue name = str(input("您的密码已经生成完毕,请重命名输出文件:")) while True: try: mode = eval(input("请选择生成密码文件的内部格式,按行存储扣1,csv文件存储扣2,json文件存储扣3:")) if mode==1: fileObject = open(name+'.txt', 'w') for each in stu_pwd_dict: fileObject.write(each+"_"+stu_pwd_dict[each]) fileObject.write('\n') fileObject.close() break elif mode==2: header = ['学号','密码'] fileObject = open(name+'.csv', 'w',encoding='UTF8', newline='') writer = csv.writer(fileObject) writer.writerow(header) for each in stu_pwd_dict: list=[each,stu_pwd_dict[each]] writer.writerow(list) fileObject.close() break elif mode==3: fileObject = open(name+'.json', 'w') fileObject.write(json.dumps(stu_pwd_dict)) fileObject.close() break else:print("您的文件格式代码输入有误,请重新输入") except: print("您的文件格式代码输入有误,请重新输入") print("感谢您的使用")
|