实验6
实验任务3
def is_valid(a): """判断身份证是否合法""" if len(a) != 18: return False else: if a[-1] == "Y": return False else: return True with open("data3_id.txt",encoding="utf-8") as f: data = f.read().split() data = [i.split(",") for i in data] data_name = [i[0] for i in data[1:]] data_ = [i[1] for i in data[1:]] ls = [] for i in data_: if is_valid(i): ls.append(i) sy = [data_.index(i) for i in ls] data_m = [i[6:14] for i in ls] data_l = [] for i in range(len(data_m)): b = data_name[sy[i]]+","+data_m[i]+"\n" data_l.append(b) data_ll = [] for i in data_l: c = i[0:7]+"-"+i[7:9]+"-"+i[9:] data_ll.append(c) with open("data_id_processed.txt","w+",encoding="utf-8") as f: f.writelines(sorted(data_ll,key=lambda x: x[3:11])) f.seek(0) print(f.read())
实验任务5
task5_1_1
import random n = int(input("输入随机抽点人数: ")) with open("data5.txt", encoding="utf-8") as f: data = f.readlines() data_ = data.copy() lucky_people = [] for i in range(n): x = random.choice(data_) lucky_people.append(x) data_.remove(x) with open("20220517.txt", "w+", encoding="utf-8") as f: f.writelines(lucky_people) f.seek(0) print(f.read())
task5_2
import random print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^")) active_input = True ls = [] while active_input: n = int(input("输入随机抽点人数: ")) if n == 0: break else: with open("data5.txt", encoding="utf-8") as f: data = f.readlines() data_ = data.copy() lucky_people = [] for i in range(n): x = random.choice(data_) lucky_people.append(x) data_.remove(x) with open("20220517.txt", "w+", encoding="utf-8") as f: f.writelines(lucky_people) f.seek(0) print(f.read()) ls += lucky_people with open("20220517.txt", "w", encoding="utf-8") as f: f.writelines(ls) print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))
实验总结
1、还要学习学习可读可写的附加模式