python读取文本内容排序

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")

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页