实验六

1,实验任务3

 1 k=['0','1','2','3','4','5','6','7','8','9']
 2 def is_valid(x):
 3     if len(x)!=18:
 4         return False
 5     else:
 6         w=0
 7         x=x.strip('X')
 8         for i in x:
 9             if i not in k:
10                 w=w+1
11         if w!=0:
12             return False
13         else:
14             return True
15 with open('data3_id.txt','r',encoding='utf-8') as f:
16     a=f.readlines()
17     b=[i.strip('\n').split(',') for i in a]
18 c=[]
19 for i in b:
20     if is_valid(i[1])==True:
21         c.append(i)
22 for i in c:
23     c.sort(key=lambda i:(int(str(i[1])[6:14])))
24 for i in c:
25     print(f'{i[0]},{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}')

运行结果:

 2,实验任务5

task5_1.py:

 1 with open('data5.txt','r',encoding='utf-8') as f:
 2     a=f.readlines()
 3     c=[]
 4     for i in a:
 5         i=i.strip('\n')
 6         c.append(i)
 7 import random
 8 x=eval(input('请输入随机抽点人数:'))
 9 d=random.sample(c,x)
10 for i in d:
11     print(i)
12 import datetime
13 t=datetime.datetime.now()
14 filename=t.strftime('%Y%m%d')+'.txt'
15 with open(filename,'w',encoding='utf-8') as f:
16     for i in d:
17         f.write(i+'\n')

运行结果:

task5_2.py:

 1 with open('data5.txt','r',encoding='utf-8') as f:
 2     a=f.readlines()
 3     c=[]
 4     for i in a:
 5         i=i.strip('\n')
 6         c.append(i)
 7 import random
 8 print('{:=^30}'.format('抽点开始'))
 9 x=eval(input('请输入随机抽点人数:'))
10 m=[]
11 while x!=0:
12     d=random.sample(c,x)
13     for i in d:
14         m.append(i)
15         print(i)
16     x=eval(input('请输入随机抽点人数:'))
17 print('{:=^30}'.format('抽点结束'))
18 import datetime
19 t=datetime.datetime.now()
20 filename=t.strftime('%Y%m%d')+'.txt'
21 with open(filename,'w',encoding='utf-8') as f:
22     e=set(m)
23     for i in e:
24         f.write(i+'\n')

运行结果:

 

posted @ 2022-05-19 11:10  炸呼  阅读(10)  评论(2编辑  收藏  举报