按照昨天的find,先去在数据里去找到qq跟密码:
class Filefind:
def __init__(self,path):#构造
self.path=path
self.file = open(self.path, "r", errors="ignore")#打开文件
self.mylist=[]
def memload(self):
self.mylist=self.file.readlines()#载入文件到列表
def memsearch(self, findstr): # 在内存的list内部查找
for line in self.mylist:
if line.find(findstr) != -1:
print(line)
def memsearchsave(self,findstr,savepath): #在内存的list内部查找
savefile=open(savepath,"w") #写入
for line in self.mylist:
if line.find(findstr)!=-1:
print(line)
savefile.write(line) #保存
savefile.close()
def disksearch(self,findstr):
while True:
mystr=self.file.readline()
if mystr.find(findstr)!=-1: #找到
print(mystr)
if not mystr:
break
def disksearchlist(self,findlist):#磁盘读取一行,list刷一遍
while True:
mystr=self.file.readline()
for findstr in findlist:
if mystr.find(findstr)!=-1: #找到
print(mystr)
if not mystr:
break
def disksearchsave(self, findstr, savepath):
savefile = open(savepath, "w") # 写入
while True:
mystr = self.file.readline()
if mystr.find(findstr) != -1: # 找到
print(mystr)
savefile.write(mystr) # 保存
if not mystr:
break
savefile.close()
def __del__(self):#销毁
self.file.close()#关闭文件
QQlist=["1005595078","360690577","365065261","809151337","89667202",
"476576775","842247839","675644697","535959419","1056315699",
"1978065375","751500924","343758340","755506077","894312019",
"403516357","2761700138","17604699"]
QQfile=Filefind(r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2\QQ.txt")
QQfile.memload() #载入内存
print("load over")
for QQ in QQlist:
QQfile.memsearch(QQ)
接下来对数据进行清洗,写这个要记住框架,是载入数据,写入数据,关闭数据,中间是写入的循环,没写入的进入badfile:
QQfilepath=r"C:\xxx"
QQfilegoodpath=r"C:\xxx"
QQfilebadpath=r"C:\xxx"
QQfile=open(QQfilepath,"r",errors="ignore")#打开文件
QQgoodfile=open(QQfilegoodpath,"w")#打开文件,写入好的
QQbadfile=open(QQfilebadpath,"w")#打开文件,写入坏的
while True: #死循环
line=QQfile.readline() #逐步读一行
if not line: #读到None,退出
break
QQlist = line.split("----")#切割每一行数据
if len(QQlist)==2:
QQlength=len(QQlist[0]) #QQ长度
Passwordlength=len(QQlist[1])#密码长度
if ( QQlength>=5 and QQlength<=11) and ( Passwordlength>=6 and Passwordlength<=16):
QQgoodfile.write(line)
else:
QQbadfile.write(line)
else:
QQbadfile.write(line) #垃圾
QQfile.close() #关闭文件
QQgoodfile.close()
QQbadfile.close()
最后就是对数据的切割:自己的理解就是先对qq号的长度进行区分,创建每一个长度的txt文件,后面就是长度匹配上就直接写入。
QQlist=[5,6,7,8,9,10,11]
#生成文件对象列表
filelist=[]
for i in QQlist:
QQpath=r"C:\Users\Tsinghua-yincheng\Desktop\yinchengDay2\QQ"
QQfilepath=QQpath+"\\"+str(i)+"位QQ.txt"
QQfile=open(QQfilepath,"w")
filelist.append(QQfile)
QQfilepath="C:\\Users\\Tsinghua-yincheng\\Desktop\\yinchengDay2\\QQ.txt"
QQfile=open(QQfilepath,"r",errors="ignore")#打开文件
while True: #死循环
line=QQfile.readline() #逐步读一行
if not line: #读到None,退出
break
QQlinelist=line.split("----")
QQlength=len(QQlinelist[0]) #求长度
for i in range(len(QQlist)): #检索下是几位
if QQlist[i]==QQlength: #匹配
filelist[i].write(line) #匹配写入
QQfile.close()
for file in filelist: #写入文件
file.close()