File size: 1,599 Bytes
f5f55b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import streamlit as st
from tname import *


def Sort_Scores(file):
    scores = []
    # file_path = st.text_input('请输入文件路径:', )
    # save_path = st.text_input('请输入文件保存路径:', )
    # if st.button('文件路径输入完成'):
    with open(file.name, "r") as f:
        contents = f.readlines()

    for i in range(2, len(contents)):
        if i % 2 == 0:
            content = contents[i].split()
            scores.append(float(content[2][6:-1]))  # 存储scores值

    na = Name()
    na = na + r"scores_sort.tsv"  # 结果文件名称

    # 按列表scores中元素的值进行排序,并返回元素对应索引序列
    sorted_id = []
    sorted_id = sorted(range(len(scores)), key=lambda k: scores[k], reverse=True)

    # 第一条序列和其他序列格式不一样,且第一条序列不需要排序,单独写入
    with open(na, "a") as f1:
        # f1.write(contents[0])
        content = contents[0].split()
        f1.write(content[0])
        f1.write("\t")
        f1.write(content[1])
        f1.write("\n")
        f1.write(contents[1])

    for i in range(0, len(scores)):
        with open(na, "a") as f1:
            content = contents[sorted_id[i] * 2 + 2].split()
            f1.write(content[0])
            f1.write("\t")
            f1.write(content[2])
            f1.write("\t")
            f1.write(content[3])
            f1.write("\n")
            # f1.write(contents[sorted_id[i] * 2 + 2])  #由于文件前两行未参与排序,所以索引要+2
            f1.write(contents[sorted_id[i] * 2 + 2 + 1])
    return na