jie1 commited on
Commit
f5f55b8
1 Parent(s): d7a6985

Upload Sort_Scores.py

Browse files
Files changed (1) hide show
  1. Sort_Scores.py +46 -0
Sort_Scores.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from tname import *
3
+
4
+
5
+ def Sort_Scores(file):
6
+ scores = []
7
+ # file_path = st.text_input('请输入文件路径:', )
8
+ # save_path = st.text_input('请输入文件保存路径:', )
9
+ # if st.button('文件路径输入完成'):
10
+ with open(file.name, "r") as f:
11
+ contents = f.readlines()
12
+
13
+ for i in range(2, len(contents)):
14
+ if i % 2 == 0:
15
+ content = contents[i].split()
16
+ scores.append(float(content[2][6:-1])) # 存储scores值
17
+
18
+ na = Name()
19
+ na = na + r"scores_sort.tsv" # 结果文件名称
20
+
21
+ # 按列表scores中元素的值进行排序,并返回元素对应索引序列
22
+ sorted_id = []
23
+ sorted_id = sorted(range(len(scores)), key=lambda k: scores[k], reverse=True)
24
+
25
+ # 第一条序列和其他序列格式不一样,且第一条序列不需要排序,单独写入
26
+ with open(na, "a") as f1:
27
+ # f1.write(contents[0])
28
+ content = contents[0].split()
29
+ f1.write(content[0])
30
+ f1.write("\t")
31
+ f1.write(content[1])
32
+ f1.write("\n")
33
+ f1.write(contents[1])
34
+
35
+ for i in range(0, len(scores)):
36
+ with open(na, "a") as f1:
37
+ content = contents[sorted_id[i] * 2 + 2].split()
38
+ f1.write(content[0])
39
+ f1.write("\t")
40
+ f1.write(content[2])
41
+ f1.write("\t")
42
+ f1.write(content[3])
43
+ f1.write("\n")
44
+ # f1.write(contents[sorted_id[i] * 2 + 2]) #由于文件前两行未参与排序,所以索引要+2
45
+ f1.write(contents[sorted_id[i] * 2 + 2 + 1])
46
+ return na