实验6

task3

 1 def is_valid(s):
 2     if len(s)!=18:
 3        return False
 4     elif s.isdigit() is False:
 5         for i in s:
 6             if i<='9' and i>='0':
 7                 continue
 8             elif i<='Z' and i>='A':
 9                 if i=='X':
10                     return True
11                 else:
12                     return False
13             else:
14                 return False
15     else:
16         return True
17 
18 
19 with open('data3_id.txt','r',encoding='utf-8')as f:
20     s=[]
21     for line in f.readlines():
22         line=line.strip('\n').split(',')
23         s.append(line)
24     del s[0]
25     ls=[]
26     for i in range(0,len(s)):
27         if is_valid(s[i][1]) is True:
28             bir=s[i][1][6:14]
29             a=bir[0:4],bir[4:6],bir[6:8]
30             a='-'.join(a)
31             s[i][1]=a
32             ls.append(s[i])
33     ls1=sorted(ls,key=lambda t:t[1],reverse=False)
34     for j in ls1:
35         print(','.join(j))

 

task5_1

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

 

task5_2

 

 1 import random
 2 while 1:
 3     n=eval(input('输入随机抽点人数:'))
 4     if n==0:
 5         print('抽点结束')
 6         break
 7     else:
 8         with open('data5.txt', 'r', encoding='utf-8') as f:
 9             ls = []
10             for line in f:
11                 ls.append(line.strip('\n'))
12             ls1 = ls.copy()
13             data=random.sample(ls,n)
14             for i in data:
15                 ls1=ls.remove(i)
16                 print(i)
17             import datetime
18             t=datetime.datetime.now()
19             name=t.strftime('%Y%m%d')+'.txt'
20             with open(name,'a',encoding='utf-8')as f:
21                 for j in data:
22                    f.write(j)
23             ls=ls1

 

posted on 2022-05-22 13:39  蒜头小兲  阅读(24)  评论(2编辑  收藏  举报

导航