实验6 文件应用编程-2

task3

 1 def is_valid(id):
 2     if len(id[1]) == 18 and (id[1][:-1].isdigit() and id[1][-1] in '0123456789X'):
 3         return True
 4     return False
 5 
 6 
 7 with open('data3_id.txt', 'r', encoding='utf-8') as f:
 8     id_name = [(i[:2], i[3:].strip('\n')) for i in f.readlines()[1:]]
 9 
10 result = list(filter(is_valid, id_name))
11 result.sort(key=lambda x: int(x[1][6:14]))
12 for i in result:
13     print(f"{i[0]},{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}")

 

截图:

 

 

 

task5_1

 1 import random
 2 
 3 n = eval(input("抽点人数: "))
 4 with open("data5.txt", "r+", encoding="UTF-8") as f:
 5     list_all = [i.strip("\n") for i in f.readlines()]
 6     
 7 menu = random.sample(list_all, n)
 8 
 9 with open("name.txt", "w+", encoding="UTF-8") as f:
10     for i in menu:
11         print(i)
12         f.write(i+"\n")

 

截图:

 

 

 

task5_2

 1 import random
 2 
 3 with open("data5.txt", "r+", encoding="UTF-8") as f:
 4     list_all = [i.strip("\n") for i in f.readlines()]
 5 
 6 print(f"{'抽点开始':=^40}")
 7 n = eval(input("随机抽人:"))
 8 while n !=0:
 9     people = random.sample(list_all, n)
10     for i in people:
11         print(i)
12         list_all.remove(i)
13     with open("name.txt", "a+", encoding="UTF-8") as f:
14         for i in people:
15             f.write(i+"\n")
16     n = eval(input("随机抽人:"))
17 print(f"{'抽点结束':=^40}")

截图:

 

posted @ 2022-05-18 17:05  柚子西虹  阅读(55)  评论(3编辑  收藏  举报