其他分享
首页 > 其他分享> > 实验六

实验六

作者:互联网

Task3:

 1 def is_vaild(x):
 2     if(len(x) != 18):
 3         return False
 4 
 5     else:
 6         for i in range(len(x)):
 7 
 8             if(x[i] < '0' or x[i] > '9'):
 9 
10                 if(x[i] == 'x' or x[i] == 'X'):
11 
12                     if(i + 1 != len(x)):
13                         return False
14                         break
15                     continue
16 
17                 else:
18                     return False
19                     break
20 
21         else:
22             return True
23 
24 def second(elem):
25     return elem[1]
26 
27 with open('实验6数据文件/data3_id.txt', 'r', encoding = 'utf-8')as f:
28     data = f.readlines()
29 
30 for i in range(len(data)):
31     data[i] = data[i].rstrip('\n').split(',')
32 x = data.pop(0)
33 
34 data2 = []
35 for i in range(len(data)):
36     if(is_vaild(data[i][1]) == True):
37         data2.append(data[i])
38 
39 for i in range(len(data2)):
40     year = data2[i][1][6:10]
41     month = data2[i][1][10:12]
42     day = data2[i][1][12:14]
43     data2[i][1] = ('{}-{}-{}'.format(year, month, day))
44 data2.sort(key = second)
45 
46 for i in range(len(data2)):
47     print(('{},{}').format(data2[i][0], data2[i][1]))

 

Task5_1:

 1 with open('实验6数据文件/data5.txt', 'r', encoding = 'utf-8')as f:
 2     data = f.readlines()
 3 
 4 import random
 5 import datetime
 6 
 7 x = set()
 8 
 9 n = eval(input('请输入你想抽的人数:'))
10 
11 while True:
12     if(len(x) < n):
13         x.add(random.randint(0,79))
14     else:
15         break
16 
17 data2 = []
18 for i in x:
19     data2.append(data[i])
20     print(data[i])
21 t = datetime.datetime.now()
22 
23 with open(t.strftime('%Y%m%d') + '.txt', 'w', encoding = 'utf-8')as f:
24     f.writelines(data2)

 

 

 

 

Task5_2:

 1 print(('{:=^40}'.format('抽点开始')))
 2 
 3 with open('实验6数据文件/data5.txt', 'r', encoding = 'utf-8')as f:
 4     data = f.readlines()
 5 
 6 import random
 7 import datetime
 8 
 9 while True:
10     n = eval(input('请输入你想抽的人数:'))
11 
12     if(n == 0):
13         break
14 
15     else:
16         x = set()
17 
18         while True:
19             if(len(x) < n):
20                 y = len(data) - 1
21                 x.add(random.randint(0, y))
22             else:
23                 break
24 
25         data2 = []
26 
27         for i in x:
28             print(data[i], end='')
29             data2.append(data[i])
30         xuhao = list(x)
31         xuhao.sort(reverse = True)
32 
33         for i in xuhao:
34             data.pop(i)
35         t = datetime.datetime.now()
36 
37         with open(t.strftime('%Y%m%d') + '-1.txt', 'a', encoding = 'utf-8')as f:
38             f.writelines(data2)
39 
40 print(('{:=^40}'.format('抽点结束')))

 

 

 

标签:True,len,datetime,实验,else,data,data2
来源: https://www.cnblogs.com/Cr2O3/p/16286078.html