pythonファイルcsvを読み込み、数値列でソート
pythonファイルcsvを読み込み、数値列でソート
最近pythonで3枚の表のマージを行い、その間にファイル列をソートし、良いコードを見つけました.
添付データは以下の通りです.
=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
#######私はAIの小さい白、各位がよけいに指導することを望みます!
テキストリンク:https://www.cnblogs.com/koujiaodahan/p/8047109.html
最近pythonで3枚の表のマージを行い、その間にファイル列をソートし、良いコードを見つけました.
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
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) + '
')
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
#######私はAIの小さい白、各位がよけいに指導することを望みます!
テキストリンク:https://www.cnblogs.com/koujiaodahan/p/8047109.html