实验6 文件应用编程
task_3
def is_valid(a): b = 0; c = 0 for i in a: b += 1 if '0' <= i <= '9' or i == 'X': pass else: c += 1 break if b != 18 or c != 0: return False if b == 18 and c == 0: return True with open("data3_id.txt","r",encoding="utf-8") as f: data = f.readlines() d = [] for line in data: d.append(line.strip('\n').split(',')) e = [] for i in d: if is_valid(i[1]): e.append(i) print(f'{d[0][0]},{d[0][1]}') e.sort(key=lambda x: x[1][6:14]) for i in e: a=[] v = i[1] x = i[0]+","+v[6:10]+"-"+v[10:12]+"-"+v[12:14] a.append(x) print(','.join(a))
task_5_1
with open("data5.txt","r",encoding="utf-8") as f: d=f.readlines() import random n=int(input("输入随机抽点人数:")) f1=set() l=len(f1) while l<n: f=random.randint(0,len(d)) f1.add(d[f]) l=len(f1) for i in f1: print(i,end="") with open("20220518.txt","w",encoding="utf-8") as f: for i in f1: f.write(i)
task_5_2
with open("data5.txt","r",encoding="utf-8") as f: d=f.readlines() import random with open("20220518_1.txt","a",encoding="utf-8") as f11: flag = 1 while (flag == 1): n = int(input("输入随机抽点人数:")) if n == 0: flag = 0 else: f1 = set() l = len(f1) while l < n: f = random.randint(0, len(d)) f1.add(d[f]) l = len(f1) for i in f1: print(i, end="") f11.write(i)