task3.py
1 def is_valid(m): 2 if len(m)==18 and m.isdigit(): 3 return 'True' 4 elif len(m)==18 and m[-1]=='X': 5 return 'True' 6 else: 7 return 'False' 8 9 with open('data3_id.txt','r',encoding = 'utf-8') as f: 10 data = f.readlines() 11 list = [] 12 for i in data: 13 data_ = i.strip('\n') 14 if is_valid(data_[3:]) == 'True': 15 list.append(data_) 16 list.sort(key=lambda x: x[9:13]) 17 for m in list: 18 print('{},{}-{}-{}'.format(m[:2],m[9:13],m[13:15],m[15:17]))
task5_2.py
1 with open('data5_1.txt','r',encoding='utf-8')as f: 2 data=f.readlines() 3 for i in data: 4 i.strip("\n") 5 import datetime 6 t=datetime.datetime.now() 7 filename=t.strftime('%Y%m%d')+'txt' 8 print(f"{'抽点开始':=^40}") 9 import random 10 while True: 11 n=int(input('输入随机抽点人数:')) 12 sample=random.sample(data,n) 13 for i in sample: 14 data.remove(i) 15 with open(filename, 'a+', encoding='utf-8') as f: 16 for i in sample: 17 print(i) 18 f.write(i) 19 if n==0: 20 print(f"{'抽点结束':=^40}") 21 break