python读取文本内容排序

1490阅读 0评论2018-11-12 chengxuyonghu
分类:系统运维

def writeLog(txtname, contests):
        f = file("/home/haoren/guojia/shanghai/%s" %txtname,'a+')
        f.write(contests)
        f.close()
        return


alldata = []
fd = open('/home/haoren/guojia/shanghai/shuju6.txt', 'r')
file1 = "shuju666.txt"
for line in fd:
    line2 = line.strip()
    duid = line2.split(',')[0]
    duhao = line2.split(',')[1]
    duid1 = line2.split(',')[2]
    shou = line2.split(',')[3]
    mian = line2.split(',')[4]
    duid2 = line2.split(',')[5]
    nicheng = line2.split(',')[6]
    #构造元祖
    data = (str(duid),str(duhao),str(duid1),str(shou),str(mian),str(duid2),str(nicheng))     alldata.append(data)
alldata1 = sorted(alldata, key=lambda alldata: int(alldata[4]) ,reverse = True)
for t in alldata1:
    t = ','.join(t)
    writeLog(file1,str(t))
    writeLog(file1,"\r\n")
上一篇:python操作elasticsearch
下一篇:python百分数小数