实验任务6:综合应用(1) —— 分数文件处理
with open('data6_1.txt','r',encoding='utf-8') as f:
ls=sorted([line.replace('\n','').split('\t') for line in f.readlines()],key=lambda k:int(k[2]),reverse=True)
with open('data6_2.txt','w+',encoding='utf-8') as nf:
for i in ls: nf.write(' '.join(i)+'\n')
with open("data6_2.txt","r",encoding="utf-8") as h:
print(h.read())
实验任务7: 综合应用(2) —— 随机抽点 任务7-1:
import random def get_lucky_boy(lis:list,trys:int): result=[] for i in range(trys): boy=random.choice(lis) lis.remove(boy) result.append(boy) t=[] for i in result: t.append(' '.join(i)+'\n') return ''.join(t) ls=[] with open('data7.txt','r',encoding='utf-8') as f: for line in f.readlines(): m=line.strip().split() ls.append(m) n=int(input('yikutu desuka?')) boys=get_lucky_boy(lis=ls,trys=n) print(boys) with open('lucky.txt','w',encoding='utf-8') as nf: nf.write(boys)
任务7-2:
import datetime import random def get_lucky_boy(lis:list,trys:int): result=[] for i in range(trys): boy=random.choice(lis) lis.remove(boy) result.append(boy) return result def ls2str(ls:list): t=[] for i in ls: t.append(' '.join(i)+'\n') return ''.join(t) with open('data7.txt','r',encoding='utf-8') as f: ls=[line.replace('\n','').split('\t') for line in f.readlines()] n=int(input('How many boyz would U like to get')) boys=get_lucky_boy(lis=ls,trys=n) result=ls2str(boys) print(result) filename=datetime.datetime.now().strftime('%Y%m%d') with open(filename,'w',encoding='utf-8') as nf: nf.write(result)