Python reads file csv, sort by 4th column, and then sort by column 5th

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.