实验六

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))

 

posted @ 2022-05-24 21:48  我真是想笑  阅读(5)  评论(0编辑  收藏  举报