实验六

def is_valid(x):
    ls=[str(i) for i in range(0,10)]
    ls.append('X')
    if len(x)<18:
        return False
    for a in range(len(x)):
        if x[a] not in ls:
            return False
            break
    if a==len(x)-1:
        return True
with open('data3_id.txt','r',encoding='utf-8')as f:
    data=f.read().split('\n')
data1=[]
data2=[]
a=0
for i in range(1,len(data)):
    x=data[i].split(',')
    if is_valid(x[1]):
        data1.append(x)
for i in data1:
    data2.append(i[1][6:14]+str(a))
    a+=1
data2.sort()
for i in range(len(data1)):
    n=int(data2[i][8:])
    print('{},{}-{}-{}'.format(data1[n][0],data2[i][0:4],data2[i][4:6],data2[i][6:8]))

with open('data5.txt','r',encoding='utf-8')as f:
    data1=f.read().split('\n')
    data2=[]
    for i in data1:
        data2.append(i.split('\t'))
import random
import datetime
while True:
  n=int(input('输入随机抽点人数:'))
  if n==0:
      break
  data3=[]
  data4=[]
  while len(data3)<n:
      if data2[random.randint(0,len(data2)-1)] not in data3:
          data3.append(data2[random.randint(0,len(data2)-1)])
  for i in data3:
      print(','.join(i))
      data4.append(','.join(i)+'\n')
  t=datetime.datetime.now()
  filename=t.strftime('%Y%m%d')+'.txt'
  with open (filename,'w',encoding='utf-8')as f:
    f.writelines(','.join(data4))

with open('data5.txt','r',encoding='utf-8')as f:
    data=f.readlines()
import random
n = eval(input('输入抽点人数: '))
while n!=0:
      n = eval(input('输入抽点人数: '))
      a=random.sample(data,n)
      b=''.join(a)
      print(b)
      import datetime
      t=datetime.datetime.now()
      filename=t.strftime('%Y%m%d')+'.txt'
      with open(f'{filename}','w',encoding='utf_8')as f:
         f.write(b)
else:
      print('抽点结束')

 

posted on 2022-05-24 22:07  唔a  阅读(0)  评论(0编辑  收藏  举报

导航