def is_valid(a): if len(a)==18 : if a.isdigit() or( a[:-1].isdigit() and a[-1]=='X'): return True else: return False else: return False with open('D://aa//data3_id.txt','r',encoding='utf-8') as f: x=f.read().strip('\n').split() x.pop(0) list=[] for i in range(len(x)): a=x[i].split(',') if is_valid(a[1])==True: list.append(a) for x in list: list.sort(key=lambda x:int(str(x[1]) [6:14])) for j in list: print(f'{j[0]},{j[1][6:10]}-{j[1][10:12]}-{j[1][12:14]}')
Task3.py
Task5_1.py
import random import datetime n=eval(input('输入随机抽点人数:')) dic=[] with open('D://aa//data5.txt','r',encoding='utf-8') as f: for i in f.readlines(): line=i.strip('\n') dic.append(line) b=random.sample(dic,n) c=datetime.datetime.now() d=c.strftime('%Y%m%d')+'.txt' with open('D://aa//20220523.txt','w',encoding='utf-8') as g: for j in b: g.write(j+'\n') print(j)
Task5_2.py
import random import datetime print('{:=^40}'.format('抽点开始')) dic=[] data=set() with open('D://aa//data5.txt','r',encoding='utf-8') as f: for i in f.readlines(): line=i.strip('\n') dic.append(line) while True: n=eval(input('输入随机抽点人数:')) if n!=0: b=random.sample(dic,n) for k in b: print(k) data.add(k) else: break print('{:=^40}'.format('抽点结束')) c=datetime.datetime.now() d=c.strftime('%Y%m%d')+'.txt' with open('D://aa//20220523.txt','w',encoding='utf-8') as g: for j in data: g.write(j+'\n')