需求分析:
有两种写法:
1. 随机生成三个数字,再随机生成3个字母,两个相加变成密码
a = random.sample(string.digits,3)
b = random,sample(string,ascii_letters,3)
password = "".join(a+b)
2.随机生成个六个数字和字母汇合的密码
p = random.sample(string.digits+string,ascii_letters,6)
password = "".join(p)
import string,random,os f = open("stus.txt",encoding="utf-8") f2 = open("stus_new.txt","w",encoding="utf-8") for line in f: line = line.strip() if line: # a = random.sample(string.digits,3) # b = random.sample(string.ascii_letters,3) # password = "".join(a+b) p = random.sample(string.ascii_letters+string.digits,6) password = "".join(p) new_line = line+','+password+"\n" f2.write(new_line) f.close() f2.close() os.remove("stus.txt") os.rename("stus_new.txt","stus.txt")