实验六

 1 def is_valid(x):
 2     if len(x)!=18:
 3         return False
 4     else:
 5         if x.isdecimal():
 6             return True
 7         elif x.find('X')and x.strip('X').isdecimal():
 8             return True
 9         else:
10             return False
11 with open('data3_id.txt','r',encoding='utf-8')as f:
12     data=f.readlines()
13 info=[]
14 for line in data:
15     info.append(line.strip('\n').split(','))    
16 info_ = []
17 for i in info:
18     if is_valid(i[1]):
19        info_.append(i) 
20 print(f'{info[0][0]},{info[0][1]}')
21 info_.sort(key=lambda x: x[1][6:14])
22 for i in info_:
23     a=[]
24     v = i[1]
25     x = i[0]+","+v[6:10]+"-"+v[10:12]+"-"+v[12:14]
26     a.append(x)
27     print(','.join(a))

5.1

 1 import random
 2 import datetime
 3 with open('data5.txt','r',encoding='utf-8')as f:
 4     data=f.readlines()
 5 data=[i.split()for i in data]
 6 n=eval(input('输入随机抽点人数:'))
 7 list=random.sample(data,n)
 8 for i in range(len(list)):
 9     print(list[i][0]+'\t'+list[i][1]+'\t'+list[i][2])
10 t=datetime.datetime.now()
11 filename=t.strftime('%Y%m%d')+'.txt'
12 with open(filename,'w',encoding='utf-8')as f:
13     for i in range(len(list)):
14         f.write(list[i][0]+'\t'+list[i][1]+'\t'+list[i][2]+'\n')

5.2

 1 import random
 2 import datetime
 3 with open('data5.txt','r',encoding='utf-8')as f:
 4     data=f.readlines()
 5 data=[i.split()for i in data]
 6 ls=[]
 7 n=1
 8 while n!=0:
 9     n=eval(input('输入随机抽点人数:'))
10     list=random.sample(data,n)
11     for i in range(len(list)):
12         print(list[i][0]+'\t'+list[i][1]+'\t'+list[i][2])
13         data.remove(list[i])
14         ls.append(list[i])
15 t=datetime.datetime.now()
16 filename=t.strftime('%Y%m%d')+'.txt'
17 with open(filename,'w',encoding='utf-8')as f:
18     for i in range(len(ls)):
19         f.write(ls[i][0]+'\t'+ls[i][1]+'\t'+ls[i][2]+'\n')

实验总结:

虽然思路是正确的,但是代码过于复杂。不过,能够自己写出一两个还是很欣慰的!

posted @ 2022-05-22 14:44  爱冒险的小洋  阅读(11)  评论(0编辑  收藏  举报