• 博客园Logo
  • 首页
  • 新闻
  • 博问
  • 专区
  • 闪存
  • 班级
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 简洁模式 ... 退出登录
    注册 登录
jinshenyao
博客园    首页    新随笔    联系   管理    订阅  订阅
实验6 文件应用编程-2

3.实验任务3

 

def is_valid(x):
    if len(x)!=18:
        return False
    else:
        if x.isdecimal():
            return True
        elif x.find('X') and x.strip('X').isdecimal():
            return True
        else:
            return False
f=open('data3_id.txt','r',encoding='utf-8')
y=f.readlines()
del y[0]
b=[]
for i in y:
    g=i.strip('\n').split(',')
    if is_valid(g[1])==True:
        b.append(g)
b.sort(key=lambda x:int(x[1][6:10]))
for i in b:
    print(f"{i[0]},{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}")
f.close()

5.实验任务5

 (1)

import random
import datetime
f=open('data5.txt','r',encoding='utf-8')
t = datetime.datetime.now()
filename = t.strftime('%Y%m%d') + '.txt'
p=open(filename,'w',encoding='utf-8')
g=f.readlines()
n=int(input('输入随机抽点人数:'))
b=random.sample(g,n)
for i in b:
    print(i.strip('\n'))
    p.write(i)
f.close()
p.close()

(2)

print(f"{'抽点开始':=^40}")
import random
import datetime
f=open('data5.txt','r',encoding='utf-8')
t = datetime.datetime.now()
filename = t.strftime('%Y%m%d') + '.txt'
p=open(filename,'w',encoding='utf-8')
g=f.readlines()
while True:
    n=int(input('输入随机抽点人数:'))
    if n==0:
        break
    else:
        b=random.sample(g,n)
        for i in b:
            print(i.strip('\n'))
            p.write(i)
f.close()
p.close()
print(f"{'抽点结束':=^40}")

 

posted on 2022-05-18 22:21  失聪的小毛  阅读(9)  评论(2)  编辑  收藏  举报
刷新评论刷新页面返回顶部
Copyright © 2022 失聪的小毛
Powered by .NET 6 on Kubernetes