实验六

2022/5/22 23:06:49

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

def is_valid(x):
    if len(x) != 18:
        return False
    else:
        for i in x:
            try:
                i = int(i)
            except:
                if ord(i) != ord('X'):
                    return False
        return True

with open('data3_id.txt', 'r', encoding='utf-8')as f:
    data = f.readlines()

data1 = [data[i].rstrip('\n').split(',')for i in range(1,len(data))]

data2 = []
for i in data1:

    if is_valid(i[1]) == True:
        data2.append(i)

for i in data2:
    i[1] = f'{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}'

data2.sort(key=lambda ele:ele[1])

for i in data2:
    print(','.join(i))

import random

n = int(input('输入随机抽点人数:'))

with open('data5.txt', 'r', encoding='utf-8')as f:
    data = random.sample(f.readlines(), n)

import datetime

t = datetime.date.today()
filename = t.strftime('%Y%m%d') + '.txt'

with open(f'{filename}', 'w+', encoding='utf-8')as f:
    f.writelines(''.join(data).rstrip('\n'))
    f.seek(0)
    print(''.join(f.readlines()))

 



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


扫一扫关注最新编程教程