实验六

3-1

1 def is_valid(x):
2         data=x
3         if len(data)!=18:
4             print(False)
5         else:
6             if ord(data[-1])==88 or 48<=ord(data[-1])<=57 :
7                 print(True)
8             else:
9                 print(False)

3-2

 1 with open('data3_id.txt', 'r', encoding='utf-8') as f:
 2     data=f.readlines()
 3 data=[line.strip().split(',') for line in data]
 4 ans=[]
 5 def isvalid(id):
 6     if len(id)!=18: return False
 7     else:
 8         for i in id:
 9             if '0'<=i<='9'or i=='X':continue
10             return False
11         return True
12 for i in range(1,len(data)):
13     if(isvalid(data[i][1])):
14         ans.append([data[i][0],data[i][1][6:10]+'-'+data[i][1][10:12]+'-'+data[i][1][12:14]])
15 ans.sort(key=lambda x:x[1])
16 print(f'{data[0][0]},{data[0][1]}')
17 for line in ans:
18     print(f'{line[0]},{line[1]}')

5-1

 1 import random
 2 import datetime
 3 with open('data5(1).txt', 'r', encoding='utf-8') as f:
 4     data=f.readlines()
 5 data=[line.strip().split(',') for line in data]
 6 book=[0]*len(data)
 7 n=eval(input('输入随机抽点人数:'))
 8 ans=[]
 9 cnt=0
10 while cnt<5:
11         x=random.randint(0,len(data))
12         if book[x]:continue
13         cnt+=1;book[x]=1
14         ans.append(data[x])
15 t=datetime.datetime.now()
16 filename = t.strftime('%Y%m%d') + '.txt'
17 for line in ans:
18     print(line[0])
19 with open(filename,'w',encoding='utf-8') as f:
20     for line in ans:
21         f.write(str(line[0]+'\n'))

5-2

 1 with open('data5(1).txt','r',encoding='utf-8')as f:
 2     data1=f.read().split('\n')
 3     data2=[]
 4     for i in data1:
 5         data2.append(i.split('\t'))
 6 import random
 7 import datetime
 8 while True:
 9   n=int(input('输入随机抽点人数:'))
10   if n==0:
11       break
12   data3=[]
13   data4=[]
14   while len(data3)<n:
15       if data2[random.randint(0,len(data2)-1)] not in data3:
16           data3.append(data2[random.randint(0,len(data2)-1)])
17   for i in data3:
18       print(','.join(i))
19       data4.append(','.join(i)+'\n')
20   t=datetime.datetime.now()
21   filename=t.strftime('%Y%m%d')+'.txt'
22   with open (filename,'w',encoding='utf-8')as f:
23     f.writelines(','.join(data4))

 

posted on 2022-05-18 17:25  lsw()  阅读(18)  评论(3编辑  收藏  举报