实验六

task5-1
import random
import datetime
with open('C:\\Users\\86131\\Desktop\\实验6数据文件\\实验6数据文件\\data5.txt','r',encoding='utf-8') as f:
    a=f.readlines()
b=[x.strip('\n').split('\t') for x in a]
x=eval(input('输入随机抽点人数:'))
t=random.sample(b,x)
for i in t:
    print(i[0],'\t',i[1],'\t',i[2])
y=datetime.datetime.now()
y=y.strftime('%y%m%d')+'.txt'
with open(y,'w',encoding='utf-8') as f:
    for i in t:
        for x in i:
            f.write(x+'\t')
        f.write('\n')

task5-2

import random
import datetime
with open('C:\\Users\\86131\\Desktop\\实验6数据文件\\实验6数据文件\\data5.txt','r',encoding='utf-8') as f:
    a=f.readlines()
b=[x.strip('\n').split('\t') for x in a]
s=1
while s!=0:
    x=eval(input('输入随机抽点人数:'))
    if x==0:
        s=0
    else:
        t=random.sample(b,x)
        for i in t:
            b.remove(i)
            print(i[0],'\t',i[1],'\t',i[2])
        y=datetime.datetime.now()
        y=y.strftime('%y%m%d')+'.txt'
        with open(y,'a',encoding='utf-8') as f:
            for i in t:
                for x in i:
                    f.write(x+'\t')
                f.write('\n')

 

posted @ 2022-05-24 16:18  雷建彬  阅读(9)  评论(2编辑  收藏  举报