实验六 文件应用编辑-2
2022/5/23 23:50:03
本文主要是介绍实验六 文件应用编辑-2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Task——3
def is_valid(x): if len(x) != 18 : return False else: for i in x: if '0'<=i<='9'or i=='X': continue return False else: return True import csv import random with open('data3_id.txt','r',encoding='utf-8')as f: data = f.readlines() data = [line.strip().split(',') for line in data] list1 = [] for i in range(1,len(data)): if(is_valid(data[i][1])): list1.append([data[i][0],data[i][1][6:10]+'-'+data[i][1][10:12]+'-'+data[i][1][12:14]]) list1.sort(key=lambda x:x[1]) print(f'{data[0][0]},{data[0][1]}') for line in list1: print(f'{line[0]},{line[1]}')
task 5.1
import random import datetime n = int(input("输入随机抽点人数: ")) with open("data5.txt", encoding="utf-8") as f: data = f.readlines() data1 = data.copy() people = [] for i in range(n): x = random.choice(data1) people.append(x) data1.remove(x) t = datetime.datetime.now() filename = t.strftime('%Y%m%d') + '.txt' with open(filename, "w+", encoding="utf-8") as f: f.writelines(people) f.seek(0) print(f.read())
task 5.2
import random import datetime t = datetime.datetime.now() filename = t.strftime('%Y%m%d') + '.txt' print("{0:{1}{3}{2}}".format("抽点开始", "=", 40, "^")) ls = [] while True: n = int(input("输入随机抽点人数: ")) if n == 0: break else: with open("data5.txt", encoding="utf-8") as f: data = f.readlines() data1 = data.copy() people = [] for i in range(n): x = random.choice(data1) people.append(x) data1.remove(x) with open(filename, "w+", encoding="utf-8") as f: f.writelines(people) f.seek(0) print(f.read()) ls += people with open(filename, "w", encoding="utf-8") as f: f.writelines(ls) print("{0:{1}{3}{2}}".format("抽点结束", "=", 40, "^"))
总结
此次实践让我对于文件编辑,能更熟练的操作,学到了更多不同的操作方式
通过random,datetime模块能更简单的完成任务。
这篇关于实验六 文件应用编辑-2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南