先看如何生成密码
itertools用来生成密码的
import itertools
mylist=("".join(x) for x in itertools.product("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",repeat=16))
如何提取密码
利用昨天所学的框架,先写好文件路径跟读入写入以及关闭,遍历文件写入第二个数据(密码)
CSDNfilepath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdn.txt"
CSDNpasswordpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpassword.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")
while True:
line=CSDNreadfile.readline() #遍历每一行
if not line:
break
csdnlist=line.split(" # ")
#print(csdnlist[1])
CSDNpasswordfile.write(csdnlist[1]+"\n") #换行\n,
CSDNreadfile.close()
CSDNpasswordfile.close()
提取用户信息
老样子先写框架,跟上面一样改一下中括号里的序号
CSDNfilepath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdn.txt"
CSDNpasswordpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnuser.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")
while True:
line=CSDNreadfile.readline() #遍历每一行
if not line:
break
csdnlist=line.split(" # ")
#print(csdnlist[1])
CSDNpasswordfile.write(csdnlist[0]+"\n") #换行\n,
CSDNreadfile.close()
CSDNpasswordfile.close()
提取邮箱
相同手法
CSDNfilepath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdn.txt"
CSDNpasswordpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnmail.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")
while True:
line=CSDNreadfile.readline() #遍历每一行
if not line:
break
csdnlist=line.split(" # ")
#print(csdnlist[1])
CSDNpasswordfile.write(csdnlist[2]) #换行\n,
CSDNreadfile.close()
CSDNpasswordfile.close()
密码排序
主要是sort这个函数
CSDNpasswordpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpassword.txt"
CSDNreadfile=open(CSDNpasswordpath,"r",errors="ignore")
passwordlist=CSDNreadfile.readlines() #密码全部读取到列表
CSDNreadfile.close()
print("读取完成")
CSDNpasswordsortpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpasswordsort.txt"
CSDNpasswritefile=open(CSDNpasswordsortpath,"w")
passwordlist.sort() #排序
print("排序完成")
for password in passwordlist: #列表写入
CSDNpasswritefile.write(password)
CSDNpasswritefile.close()
统计次数
刚刚完成的密码排序,同时也作为判断次数的一个标准进行比对。
CSDNpasswordsortpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpasswordsort.txt"
CSDNreadpasswordfile=open(CSDNpasswordsortpath,"r",errors="ignore")#读取文件
passlist= CSDNreadpasswordfile.readlines()
CSDNreadpasswordfile.close()
print("读取完成")
savefilepath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpasswordsorttimes.txt"
savefile=open(savefilepath,"w")
length=len(passlist)
i=0
while i<length-1:#防止越界
times=1 #统计次数
password=passlist[i]#记录下密码
while i+1<=length-1 and passlist[i]==passlist[i+1]:#去除重复
times+=1 #累加次数
i+=1 #继续前进
savefile.write(str(times)+" # "+password)
#print(password,times)
i = i + 1
savefile.close()
按照次数进行排序
passtimepath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnpasswordsorttimes.txt"
passfile=open(passtimepath,"r",errors="ignore")
passtimelist=passfile.readlines()#读取次数与密码到列表
passfile.close()
print("读取完成")
#["4 # .369.369","1 # .3690147"] passtimelist
#[[4,“369.369”],[1,“3690147”]] newlist
#数据再次加工,
newlist=[]
for passtimes in passtimelist:
linelist=passtimes.split(" # ")
newlinelist=[eval(linelist[0]),linelist[1]] #字符串转化为整数
newlist.append(newlinelist) #[[4,“369.369”],[1,“3690147”]] newlist
print("数据加工完成")
newlist.sort(key=lambda x:x[0]) # x=[4,“369.369”] x[0]=4
newlist.reverse()#从大到小
print("排序完成")
CSDNpasswordsortpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2Down\csdnlastpassword.txt"
CSDNpasswritefile=open(CSDNpasswordsortpath,"w")
print("排序完成")
for password in newlist: #列表写入
CSDNpasswritefile.write(str(password[0])+" # "+password[1])
CSDNpasswritefile.close()
## 去掉重复统计次数逻辑
>
mylist=["a","a","a","b","c","c","c","d","e","e"]
length=len(mylist)#列表长度
i=0
while i<length-1:#防止越界
times=1 #统计次数
password=mylist[i]#记录下密码
while i+1<=length-1 and mylist[i]==mylist[i+1]:#去除重复
times+=1 #累加次数
i+=1 #继续前进
print(password,times)
i=i+1