实验六


with open('data3_id.txt', 'r', encoding='utf-8') as f:
    file = f.readlines()
    file.pop(0)
    id_name = [(i[:2], i[3:].strip('\n')) for i in file]
    def is_valid(id):
        if len(id[1]) == 18 and (
                set(id[1]) - set('0123456789X') in set('0123456789X') or set(id[1]) - set('0123456789X') == set()):
            return True
        return False
    result = list(filter(is_valid, id_name))
    result.sort(key=lambda x: int(x[1][6:14]))
    for i in result:
        print(f"{i[0]},{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}")

 

task5_1
import random
with open('data5.txt', 'r+', encoding = 'utf-8') as f,open('20220518.txt','r+',encoding='utf-8')as ff:
    data=f.readlines()
    y=input("请输入随机抽取人数: ")
    x=random.sample(data,int(y))
    z=''.join(x).strip('\n')
    print(z)
    ff.write(z)

task5_2
import random
with open('data5.txt', 'r+', encoding = 'utf-8') as f,open('20220518.txt','r+',encoding='utf-8')as ff:
    data=f.readlines()
    s=0
    while s==0:
        y=int(input('请输入随机抽取人数:'))
        if y==0:
            break
        else:
            x=random.sample(data,int(y))
            z=''.join(x).strip('\n')
            print(z)
            ff.write(z)

 

posted @ 2022-05-18 19:33  云处安  阅读(12)  评论(2编辑  收藏  举报