• 欢迎光临~

实验6 文件应用编程-2

开发技术 开发技术 2022-05-23 次浏览
def is_valid(x):
    if len(x)==18:
        if x.isdigit() or( x[:-1].isdigit() and x[-1]=='X'):
            return True
        else:
            return False
    else:
        return False

with open('data3_id.txt','r',encoding='utf-8')as f:
    data=f.readlines()
    a=[]
    for i in range(1,len(data)):
        if is_valid(data[i][3:-1]):
            a.append(data[i])
    a=sorted(a,key=lambda x:(x[-13:-9],x[-9:-7],x[-7:-5]))
    for i in a:
        print(f'{i[:2]},{i[-13:-9]}-{i[-9:-7]}-{i[-7:-5]}')

实验6 文件应用编程-2

import random
import datetime
with open('data5.txt','r',encoding='utf-8')as f:
    data=f.readlines()
data=[i.split()for i in data]
n=eval(input('输入随机抽点人数:'))
list=random.sample(data,n)
for i in range(len(list)):
    print(list[i][0]+'t'+list[i][1]+'t'+list[i][2])
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(list)):
        f.write(list[i][0]+'t'+list[i][1]+'t'+list[i][2]+'n')

实验6 文件应用编程-2

import random
import datetime
with open('data5.txt','r',encoding='utf-8')as f:
    data=f.readlines()
data=[i.split()for i in data]
ls=[]
n=1
while n!=0:
    n=eval(input('输入随机抽点人数:'))
    list=random.sample(data,n)
    for i in range(len(list)):
        print(list[i][0]+'t'+list[i][1]+'t'+list[i][2])
        data.remove(list[i])         
        ls.append(list[i])
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(ls)):
        f.write(ls[i][0]+'t'+ls[i][1]+'t'+ls[i][2]+'n')

实验6 文件应用编程-2

 

程序员灯塔
转载请注明原文链接:实验6 文件应用编程-2
喜欢 (0)