实验六
实验任务3:
def is_valid(line): if len(line)==18: if line.isdigit() or line[-1]=='X': return True else: return False else: return False with open('data3_id.txt','r',encoding='utf-8')as f: data=f.read().split('\n') data1=[] data2=[] a=0 for i in range(1,len(data)): x=data[i].split(',') if is_valid(x[1]): data1.append(x) for i in data1: data2.append(i[1][6:14]+str(a)) a+=1 data2.sort() for i in range(len(data1)): n=int(data2[i][8:]) print('{},{}-{}-{}'.format(data1[n][0],data2[i][0:4],data2[i][4:6],data2[i][6:8]))
实验任务5:
import random import datetime t=eval(input('输入随机抽点人数:')) with open('data5.txt','r',encoding='utf-8') as f: data=[i.strip('\n') for i in f.readlines()] a=random.sample(data,t) b=datetime.datetime.now() c=b.strftime('%Y%m%d')+'.txt' with open(f'{c}','w',encoding='utf-8') as f: for i in a: f.write(i+'\n') print(i)
import random import datetime with open('data5.txt', 'r', encoding='utf-8') as f: data = [i.strip('\n') for i in f.readlines()] print('{:=^40}'.format('抽点开始')) while True: t = eval(input('输入随机抽点人数:')) if t==0: print('{:=^40}'.format('抽点结束')) break else: a = random.sample(data, t) for i in a: print(i) data.remove(i) b = datetime.datetime.now() c = b.strftime('%Y%m%d') + '.txt' with open(f'{c}', 'w', encoding='utf-8') as f: for i in a: f.write(i + '\n')