python读取文件csv,先按第4列排序,再按第5列的数值排序
插入代码:
from operator import itemgetter input_file = open("PDBhaemoglobinReport.csv") output_file = open("PDBhaemoglobinSorted.csv","w") table = [] header = input_file.readline() #读取并弹出第一行 for line in input_file: col = line.split(',') #每行分隔为列表,好处理列格式 col[3] = float(col[3][1:-1]) col[4] = int(col[4][1:-2]) #各行没有先strip 末位是\n table.append(col) #嵌套列表table[[8,8][*,*],...] table_sorted = sorted(table, key=itemgetter(3, 4))#先后按列索引3,4排序 output_file.write(header + '\t') for row in table_sorted: #遍历读取排序后的嵌套列表 row = [str(x) for x in row] #转换为字符串格式,好写入文本 output_file.write("\t".join(row) + '\n') input_file.close() output_file.close()
附:
PDBhaemoglobinReportcsv内容
PDB ID,Chain ID,Exp. Method,Resolution,Chain Length
"1A4F","A","X-RAY DIFFRACTION","2.00","141"
"1C7C","A","X-RAY DIFFRACTION","1.80","283"
"1CG5","A","X-RAY DIFFRACTION","1.60","141"
"1FAW","A","X-RAY DIFFRACTION","3.09","141"
"1HDA","A","X-RAY DIFFRACTION","2.20","141"
"1IRD","A","X-RAY DIFFRACTION","1.25","141"
"1KFR","A","X-RAY DIFFRACTION","1.85","147"
"1QPW","A","X-RAY DIFFRACTION","1.80","141"
"1SPG","A","X-RAY DIFFRACTION","1.95","144"
"1UX8","A","X-RAY DIFFRACTION","2.15","132"
PDBhaemoglobinSorted.csv :
PDB ID,Chain ID,Exp. Method,Resolution,Chain Length
"2W72" "A" "X-RAY DIFFRACTION" 1.07 141
"1IRD" "A" "X-RAY DIFFRACTION" 1.25 141
"2H8F" "A" "X-RAY DIFFRACTION" 1.3 143
"2WY4" "A" "X-RAY DIFFRACTION" 1.35 140
"2D5X" "A" "X-RAY DIFFRACTION" 1.45 141
"4ESA" "A" "X-RAY DIFFRACTION" 1.45 143
"3VRG" "A" "X-RAY DIFFRACTION" 1.5 141
"1CG5" "A" "X-RAY DIFFRACTION" 1.6 141
"4HBI" "A" "X-RAY DIFFRACTION" 1.6 146
"1QPW" "A" "X-RAY DIFFRACTION" 1.8 141
本文来自博客园,作者:BioinformaticsMaster,转载请注明原文链接:https://www.cnblogs.com/koujiaodahan/p/8047109.html
posted on 2017-12-16 17:22 BioinformaticsMaster 阅读(10835) 评论(0) 编辑 收藏 举报