实验六

 1 def is_valid(x):
 2     ls=[str(i) for i in range(0,10)]
 3     ls.append('X')
 4     if len(x)<18:
 5         return False
 6     for a in range(len(x)):
 7         if x[a] not in ls:
 8             return False
 9             break
10     if a==len(x)-1:
11         return True
12 with open('data3_id.txt','r',encoding='utf-8')as f:
13     data=f.read().split('\n')
14 data1=[]
15 data2=[]
16 a=0
17 for i in range(1,len(data)):
18     x=data[i].split(',')
19     if is_valid(x[1]):
20         data1.append(x)
21 for i in data1:
22     data2.append(i[1][6:14]+str(a))
23     a+=1
24 data2.sort()
25 for i in range(len(data1)):
26     n=int(data2[i][8:])
27     print('{},{}-{}-{}'.format(data1[n][0],data2[i][0:4],data2[i][4:6],data2[i][6:8]))

 1 with open('data5.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 @ 2022-05-24 10:19  张博睿  阅读(7)  评论(1编辑  收藏  举报