Insert Code:
fromoperatorImportItemgetterinput_file= Open ("Pdbhaemoglobinreport.csv") Output_file= Open ("Pdbhaemoglobinsorted.csv","W") Table=[]header=input_file.readline () #读取并弹出第一行 forLineinchInput_file:col= Line.split (',') #每行分隔为列表 to handle column-style col[ .3] = float (col[3][1:-1]) col[4] = Int (col[4][1:-2])#the lines are not first strip and the bottom is \ nTable.append (COL) #嵌套列表table [[8,8][*,*],...]table_sorted= sorted (table, Key=itemgetter (3, 4)) #先后按列索引3, 4 sort output_file.write (header+'\ t') forRowinchtable_sorted: #遍历读取排序后的嵌套列表 row= [STR (x) forXinchRow] #转换为字符串格式 to write text Output_file.write ("\ t". Join (Row) +'\ n') Input_file.close () output_file.close ()
Report:
Pdbhaemoglobinreportcsv Content
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.07141
"1IRD" "A" "X-ray diffraction" 1.25141
"2h8f" "A" "X-ray diffraction" 1.3143
"2wy4" "A" "X-ray diffraction" 1.35140
"2d5x" "A" "X-ray diffraction" 1.45141
"4ESA" "A" "X-ray diffraction" 1.45143
"3VRG" "A" "X-ray diffraction" 1.5141
"1cg5" "A" "X-ray diffraction" 1.6141
"4HBI" "A" "X-ray diffraction" 1.6146
"1QPW" "A" "X-ray diffraction" 1.8141
Python reads file csv, sort by 4th column, and then sort by column 5th