python简单的数据处理(文件)

1.数据分割


CSDNfilepath=r"这里填要处理的文件路径"
CSDNpasswordpath=r"这里自定义截取密码后的路径\截取密码.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")
while True:
    line = CSDNreadfile.readline()
    if not line:
        break
    csdnList = line.split(" # ")
    CSDNpasswordfile.write(csdnList[1]+"\n")

CSDNreadfile.close()
CSDNpasswordfile.close()

2.数据排序

对截取的密码数据进行排序,以便后边查重

CSDNfilepath=r"这里是截取好密码的路径\截取密码.txt"
CSDNpasswordpath=r"这里自定义对密码排序后要保存的路径\密码排序1.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")

csdnList = CSDNreadfile.readlines()
csdnList.sort()

for pwd in csdnList:
    CSDNpasswordfile.write(pwd)
CSDNreadfile.close()
CSDNpasswordfile.close()

3.分析密码出现次数

CSDNfilepath=r"这里是对密码排序后的路径\密码排序1.txt"
CSDNpasswordpath=r"这里自定义对密码判断出现次数的路径\判断出现次数.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")

csdnList = CSDNreadfile.readlines()
pwdLength = len(csdnList)

i = 0
while i < pwdLength:
    times = 1
    while i+1 <= pwdLength-1 and csdnList[i] == csdnList[i+1]:
        times += 1
        i += 1
    CSDNpasswordfile.write(str(times)+" # "+csdnList[i])
    i += 1

CSDNreadfile.close()
CSDNpasswordfile.close()

4.根据次数进行排序

CSDNfilepath=r"这里是对密码判断出现次数的路径\判断出现次数.txt"
CSDNpasswordpath=r"这里自定义对密码判断出现次数排序的路径\出现次数排序.txt"
CSDNreadfile=open(CSDNfilepath,"r",errors="ignore")
CSDNpasswordfile=open(CSDNpasswordpath,"w")

newList = []
csdnList = CSDNreadfile.readlines()
for line in csdnList:
    lineList = line.split(" # ")
    newLineList = [eval(lineList[0]),lineList[1]]
    newList.append(newLineList)

newList.sort(key=lambda x:x[0])
newList.reverse()

for pwd in newList:
    CSDNpasswordfile.write(str(pwd[0]) + "," + pwd[1])

CSDNreadfile.close()
CSDNpasswordfile.close()

你可能感兴趣的:(python)