def is_valid(m): if len(m)==18 and m.isdigit(): return 'True' elif len(m)==18 and m[-1]=='X': return 'True' else: return 'False' with open('D:/实验六数据文件data3_id.txt','r',encoding = 'utf-8') as f: data = f.readlines() list = [] for i in data: data_ = i.strip('\n') if is_valid(data_[3:]) == 'True': list.append(data_) list.sort(key=lambda x: x[9:13]) for m in list: print('{},{}-{}-{}'.format(m[:2],m[9:13],m[13:15],m[15:17]))
import random import datetime t = datetime.datetime.now() m = eval(input('输入随机抽点人数:')) with open('D:/实验六数据文件data5.txt', encoding = 'utf-8') as f:
data = f.readlines() list = [] for i in range(m): x = random.choice(data) list.append(x) data.remove(x) filename = t.strftime('%Y%m%d') + '.txt' with open(filename, "w+", encoding="utf-8") as f: f.writelines(list) f.seek(0) print(f.read())
with open('D:/实验六数据文件data5_1.txt','r',encoding='utf-8')as f: data=f.readlines() for i in data: i.strip("\n") import datetime t=datetime.datetime.now() filename=t.strftime('%Y%m%d')+'txt' print(f"{'抽点开始':=^40}") import random while True: n=int(input('输入随机抽点人数:')) sample=random.sample(data,n) for i in sample: data.remove(i) with open(filename, 'a+', encoding='utf-8') as f: for i in sample: print(i) f.write(i) if n==0: print(f"{'抽点结束':=^40}") break