实验四
实验内容:
1. 实验任务6
with open('data6_1.txt','r',encoding='utf-8') as f: d=f.read().split('\n') d1=[str(n).split("\t") for n in d] d1.sort(key=lambda x: x[2],reverse=True) with open('data6_2.txt','w+',encoding='utf-8') as f: for line in d1: for n in line: f.write(n+'\t') print(n+'\t',end='') f.write('\n') print()
2.实验任务7
7.1
import random with open('data7.txt','r',encoding='gbk')as f: data=f.read().split('\n') n=eval(input('输入随机抽点人数:')) m=set() while len(m) < n: ra=random.randint(0,len(data)-1) m.add(str(data[ra])) with open('lucky.txt','w+',encoding='utf-8')as f: for line in m: f.write(line +'\n') print(line)
7.2
import datetime time=datetime.datetime.now() time=time.strftime('%Y%m%d') import random with open('data7.txt','r',encoding='gbk')as f: data=f.read().split('\n') n=eval(input('输入随机抽点人数:')) m=set() while len(m) < n: ra=random.randint(0,len(data)-1) m.add(str(data[ra])) with open(f'{time}.txt','w+',encoding='utf-8')as f: for line in m: f.write(line +'\n') print(line)