Java教程

实验6 文件应用编程-2

本文主要是介绍实验6 文件应用编程-2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
def  is_valid(a):
    if len(a)==18 :
        if a.isdigit() or( a[:-1].isdigit() and a[-1]=='X'):
             return True
        else:
             return False
    else:
         return False
with open('D://aa//data3_id.txt','r',encoding='utf-8') as f:
    x=f.read().strip('\n').split()
    x.pop(0)
    list=[]
    for i in range(len(x)):
        a=x[i].split(',')
        if is_valid(a[1])==True:
            list.append(a)
    for x in list:
        list.sort(key=lambda x:int(str(x[1]) [6:14]))
    for j in list:
     print(f'{j[0]},{j[1][6:10]}-{j[1][10:12]}-{j[1][12:14]}')

Task3.py

Task5_1.py

import random
import datetime
n=eval(input('输入随机抽点人数:'))
dic=[]
with open('D://aa//data5.txt','r',encoding='utf-8') as f:
        for i in f.readlines():
            line=i.strip('\n')
            dic.append(line)
b=random.sample(dic,n)
c=datetime.datetime.now()
d=c.strftime('%Y%m%d')+'.txt'
with open('D://aa//20220523.txt','w',encoding='utf-8') as g:
    for j in b:
        g.write(j+'\n')
        print(j)

 

Task5_2.py

import random
import datetime
print('{:=^40}'.format('抽点开始'))
dic=[]
data=set()
with open('D://aa//data5.txt','r',encoding='utf-8') as f:
    for i in f.readlines():
        line=i.strip('\n')
        dic.append(line)
while True:
    n=eval(input('输入随机抽点人数:'))
    if n!=0:
       b=random.sample(dic,n)
       for k in b:
          print(k)
          data.add(k)
    else:
        break

print('{:=^40}'.format('抽点结束'))
c=datetime.datetime.now()
d=c.strftime('%Y%m%d')+'.txt'
with open('D://aa//20220523.txt','w',encoding='utf-8') as g:
    for j in data:
        g.write(j+'\n')

 

这篇关于实验6 文件应用编程-2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!