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

实验六

作者:互联网

task3
def is_valid(x):
    if len(x) != 18:
        return False
    for i in x:
        if not (i.isdigit() or i == 'X'):
            return False
    else:
        return True

import csv
l=[]
with open('data3_id.txt', 'r', encoding='utf-8') as f:
    f_reader = csv.DictReader(f)
    for line in f_reader:
        n=line.get('身份证号码')
        name=line.get('姓名')
        if is_valid(n) is True:
            x=(f'{name},{n[6:10]}-{n[10:12]}-{n[12:14]}')
            l.append(x)
    l1=sorted(l,key=(lambda x : x[3:8]),reverse=False)
    for i in l1:
        print(i)

 

 

 task 5.1

import random
import datetime
data1=[]

with open('data5.txt', 'r', encoding='utf-8') as f:
    data=f.readlines()
    for i in data:
        a=i.strip('\n')
        data1.append(a)
    n=eval(input('输入随机抽取人数'))
    R=random.sample(data1,n)
    for i in R:
        print(i)
t=datetime.datetime.now()
T=t.strftime('%Y%m%d')
filename=T+'.txt'
with open (filename,'w',encoding='utf-8')as f:
    for i in R:
        f.write(i+'\n')

 

 

 

 

 

 

 task5.2

import random
import datetime
data1=[]

with open('data5.txt', 'r', encoding='utf-8') as f:
    data=f.readlines()
    for i in data:
        a=i.strip('\n')
        data1.append(a)


t=datetime.datetime.now()
T=t.strftime('%Y%m%d')
filename=T+'.txt'
S=set()
while True:
    print('{:=^40}'.format('抽检开始'))
    n=eval(input('输入随机抽取人数'))
    if n == 0 :
        print('{:=^40}'.format('抽检结束'))
        break
    R=random.sample(data1,n)
    for i in R:
        data1.remove(i)
    for i in R:
        print(i)
    t=datetime.datetime.now()
    T=t.strftime('%Y%m%d')
    filename=T+'.txt'
    with open (filename,'a',encoding='utf-8')as f:
        for i in R:
            f.write(i+'\n')

 

标签:encoding,open,filename,datetime,实验,txt,data1
来源: https://www.cnblogs.com/Lilghost/p/16305534.html