实验6 文件应用编程-2

task3.py

 1 def is_valid(y):
 2     if len(y)==18:
 3         y1=y[:17]
 4         if y1.isdigit():
 5             for j in y:
 6                 y2=[]
 7                 y2.append(j)
 8             m=['1','2','3','4','5','6','7','8','9','X']
 9             if y[-1] in m:
10                 return True
11             else:
12                 return False
13         else:
14             return False
15     else:
16         return False
17 with open('data3_id.txt','r',encoding='utf-8')as f:
18     data=f.readlines()
19 list=[]
20 for i in data:
21     datas=i.strip('\n')
22     if is_valid(datas[3:])==True:
23         list.append(datas)
24 list.sort(key=lambda y:y[9:13])
25 for m in list:
26     print(f'{m[:2]},{m[9:13]}-{m[13:15]}-{m[15:17]}')

task5.1.py

 1 import random
 2 import datetime
 3 t = datetime.datetime.now()
 4 m = eval(input('输入随机抽点人数:'))
 5 with open('data5.txt', encoding = 'utf-8') as f:
 6     data = f.readlines()
 7 list = []
 8 for i in range(m):
 9     x = random.choice(data)
10     list.append(x)
11     data.remove(x)
12 filename = t.strftime('%Y%m%d') + '.txt'
13 with open(filename, "w+", encoding="utf-8") as f:
14     f.writelines(list)
15     f.seek(0)
16     print(f.read())

task5.2.py

 1 import random
 2 import datetime
 3 t = datetime.datetime.now()
 4 print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^"))
 5 list_ = []
 6 while True:
 7     n = eval(input("输入随机抽点人数: "))
 8     if n == 0:
 9         break
10     else:
11         with open("data5.txt", encoding="utf-8") as f:
12             data = f.readlines()
13         list = []
14         for i in range(n):
15             x = random.choice(data)
16             list.append(x)
17             data.remove(x)
18         with open("20220517.txt", "w+", encoding="utf-8") as f:
19             f.writelines(list)
20             f.seek(0)
21             print(f.read())
22     list_ += list
23 filename = t.strftime('%Y%m%d') + '.txt'
24 with open(filename, "w", encoding="utf-8") as f:
25     f.writelines(list_)
26 print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))

 

posted @ 2022-05-24 13:42  虞渊  阅读(14)  评论(2编辑  收藏  举报