实验6

task3.py

def is_valid(n):
    if len(n) == 18:
        if (n.isdigit()) or (n[:-1].isdigit() and n[-1] == 'X'):
            return True
        else:
            return False
    else:
        return False


with open('data3_id.txt','r',encoding='utf-8') as f:
    data = f.readlines()
d = [line.strip('\n').split(',') for line in data]
d.remove(d[0])
d.sort(key=lambda a:int(a[1][6:14]))
for t in d:
    if is_valid(t[1]) is True:
        year = t[1][6:10]
        month = t[1][10:12]
        day = t[1][12:14]
        print(f'{t[0]},{year}-{month}-{day}')

task5_1.py

with open('data5.txt', 'r', encoding='utf-8') as f:
data = f.read().split('\n')
import random
x = eval(input('输入随机抽点人数:'))
x1 = random.sample(data, x)
import datetime
t = datetime.datetime.now()
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, 'w', encoding='utf-8') as f:
for i in x1:
f.write(i+'\n')
print(i)

task5_2.py

with open('data5.txt', 'r', encoding='utf-8') as f:
    data = f.read().split('\n')
import random
import datetime
t = datetime.datetime.now()
filename = t.strftime('%Y%m%d') + '.txt'
with open(filename, 'w', encoding='utf-8') as f:
    print("{:=^30}".format('抽点开始'))
    x = 1
    while x != 0:
        x = eval(input('输入随机抽点人数:'))
        x1 = random.sample(data, x)
        for i in x1:
            f.write(i + '\n')
            print(i)
    print("{:=^30}".format('抽点结束'))

posted on 2022-05-22 15:46  好运来。  阅读(20)  评论(2编辑  收藏  举报