task1-1

 

 task1-2

 

 task1-3

 

 

task2-1

 

 task2-2

 

 

task2-3

 

 task2-4

 

 

task3

 

def is_valid(m):
    if len(m)==18 and m.isdigit():
        return '合法'
    elif len(m)==18 and m[-1]=='X':
        return '合法'
    else:
        return '不合法'

with open('data3_id.txt','r',encoding = 'utf-8') as f:
    data = f.readlines()
list = []
for i in data:
    data_ = i.strip('\n')
    if is_valid(data_[3:]) == '合法':
        list.append(data_)
list.sort(key=lambda x: x[9:13])
for m in list:
    print('{},{}-{}-{}'.format(m[:2],m[9:13],m[13:15],m[15:17]))

 

 task5-1

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

 

 task5-2

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 on 2022-05-29 15:30  橡皮小飒  阅读(20)  评论(0编辑  收藏  举报