实验6 文件应用编程-2

2022/5/24 1:19:56

本文主要是介绍实验6 文件应用编程-2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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]}')

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')

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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程