实验6 文件应用编程-2

def is_valid(y):
    if len(y)==18:
        y1=y[:17]
        if y1.isdigit():
            for j in y:
                y2=[]
                y2.append(j)
            m=['1','2','3','4','5','6','7','8','9','X']
            if y[-1] in m:
                return True
            else:
                return False
        else:
            return False
    else:
        return False
with open('data3_id.txt','r',encoding='utf-8')as f:
    data=f.readlines()
list=[]
for i in data:
    datas=i.strip('\n')
    if is_valid(datas[3:])==True:
        list.append(datas)
list.sort(key=lambda y:y[9:13])
for m in list:
    print(f'{m[:2]},{m[9:13]}-{m[13:15]}-{m[15:17]}')

 

 

import random
import datetime
t = datetime.datetime.now()
m = eval(input('输入随机抽点人数:'))
with open('data5.txt', encoding = 'utf-8') as f:
    data = f.readlines()
list = []
for i in range(m):
    x = random.choice(data)
    list.append(x)
    data.remove(x)
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, "w+", encoding="utf-8") as f:
    f.writelines(list)
    f.seek(0)
    print(f.read())

 

 

 

 

import random
import datetime
t = datetime.datetime.now()
print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^"))
list_ = []
while True:
    n = eval(input("输入随机抽点人数: "))
    if n == 0:
        break
    else:
        with open("data5.txt", encoding="utf-8") as f:
            data = f.readlines()
        list = []
        for i in range(n):
            x = random.choice(data)
            list.append(x)
            data.remove(x)
        with open("20220517.txt", "w+", encoding="utf-8") as f:
            f.writelines(list)
            f.seek(0)
            print(f.read())
    list_ += list
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, "w", encoding="utf-8") as f:
    f.writelines(list_)
print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))

 

posted @ 2022-05-24 14:41  东楼贺朝  阅读(8)  评论(0编辑  收藏  举报