# 实验6

实验任务3
def is_valid(s):
if(len(s)!=18):
return False
for i in range(len(s)):
if((s[i]<'0' or s[i]>'9') and s[i]!='X'):
return False
else:
return True
li=[]
with open('data3_id.txt','r',encoding='utf-8')as f:
for line in data[1:]:
if(is_valid(line[3:].strip())==True):
li.append(line)
list=[]
for i in range(len(li)):
list.append(li[i][0:3]+li[i][9:13]+'-'+li[i][13:15]+'-'+li[i][15:17])
for i in range(len(list)):
list[i]=list[i].split(',')
list[i]=tuple(list[i])
list1=sorted(list,key=lambda x:(x[1],x[0]))
for i in range(len(list1)):
print(list1[i][0]+','+list1[i][1])

实验任务5
5.1
with open('data5.txt','r',encoding='utf-8')as f:
list=[line.split()for line in list]
import random
n=eval(input('输入随机抽点人数：'))
Is=random.sample(list,n)
for i in range(len(Is)):
print(Is[i][0]+'\t'+Is[i][1]+'\t'+Is[i][2])
import datetime
t=datetime.datetime.now()
filename=t.strftime('%Y%m%d')+'.txt'
with open(filename,'w',encoding='utf-8')as f:
for i in range(len(Is)):
f.write(Is[i][0]+'\t'+Is[i][1]+'\t'+Is[i][2]+'\n')



5.2
with open('data5.txt','r',encoding='utf-8')as f:
list=[line.split()for line in list]
import random
n=1
print('{:=^40}'.format('抽点开始'))
li=[]
while n!=0:
n=eval(input('输入随机抽点人数：'))
Is=random.sample(list,n)
for i in range(len(Is)):
print(Is[i][0]+'\t'+Is[i][1]+'\t'+Is[i][2])
list.remove(Is[i])
li.append(Is[i])
import datetime
t=datetime.datetime.now()
filename=t.strftime('%Y%m%d')+'.txt'
with open(filename,'w',encoding='utf-8')as f:
for i in range(len(li)):
f.write(li[i][0]+'\t'+li[i][1]+'\t'+li[i][2]+'\n')
print('{:=^40}'.format('抽点结束'))

posted @ 2022-05-22 08:27  202183360008仲苏洋  阅读(13)  评论(1编辑  收藏  举报