实验6 文件应用编程2

实验3

with open('data3_id.txt','r') as f:
    a=f.readlines()
    file=[]
    for i in a:
        file.append(i.strip('\n').split(','))
        file1=file[1::]
num=['0','1','2','3','4','5','6','7','8','9','X']

def is_valid(n):
    i=1
    if len(n)!=18:
        return False
    else:
        for j in n:
            if j not in num:
                i+=1
                if i!=0:
                    return False
                else:
                    return True
   

for i in file1:
    if is_valid(i[1])==False:
        file1.remove(i)
b=[]
for i in file1:
    c=[i[1][6:14],i[0]]
    b.append(c)
    d=sorted(b)
for i in d:
       print(f'{i[1]},{i[0][0:4]}-{i[0][4:6]}-{i[0][6:8]}')

实验5

import random
import datetime
t=datetime.datetime.now()
filename=t.strftime('%Y%m%d')+'.txt'


with open('data5.txt','r') as f:
    namelist=f.readlines()

num=[i for i in range(0,len(namelist))]
re=[]
while True:
    n=input('输入随机点名人数:')
    if int(n)==0:
        break
    d=1
    while d<=int(n):
        a=random.randint(0,len(namelist)-1)
        if a in re:
            a=random.randint(0,len(namelist)-1)
            re.append(a)
            print(namelist[a])
        else:
            re.append(a)
            print(namelist[a])
        d+=1
with open(filename,'w+') as f:     
    for i in re:
        f.write(namelist[i])

 

 

posted @ 2022-05-18 22:34  Embeddd  阅读(12)  评论(3编辑  收藏  举报