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

实验六

作者:互联网

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

 

标签:data3,实验,ls,import,data1,append,data2
来源: https://www.cnblogs.com/wozhenshixiangxiao/p/16307335.html