jie_test4 / Sort_Scores.py
jie1's picture
Upload Sort_Scores.py
f5f55b8
raw
history blame
1.6 kB
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