QQ的数据清洗

按照昨天的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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青灯有味是儿时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值