实验内容
实验任务1,2,4,6依要求略。
实验任务3
1 def task3(): 2 def is_valid(num): 3 real=1 4 for i in num: 5 if not(("0"<=i<="9" or i=="X") and len(num)==18): 6 real=0;break 7 8 if real: 9 return True 10 else: 11 return False 12 13 with open("/Users/chenxi/Documents/文稿 - 陈曦的MacBook Air - 1/python data/实验6数据文件/data3_id.txt", 'r', encoding="utf-8") as f: 14 title=f.readline() 15 datas=f.readlines() 16 data=[] 17 for i in datas: 18 i=i.rstrip().split(",") 19 if is_valid(i[1]): 20 data.append((i[0],i[1])) 21 22 print(data) 23 print(data[0][1]) 24 25 for i in range(len(data)): 26 for j in range(i+1,len(data)): 27 if eval(data[i][1][6:14])>eval(data[j][1][6:14]): 28 data[i],data[j]=data[j],data[i] 29 30 for i in data: 31 print(f"{i[0]},{i[1][6:10]}-{i[1][10:12]}-{i[1][12:14]}")
运行截图
*此处排序算法为本人手写冒泡排序,结果与所给实验结果略有出入,示例应当使用了其他排序算法。
实验任务5
1 def task5(): 2 import random 3 import datetime 4 5 n=eval(input("input roll call number:")) 6 with open("/Users/chenxi/Documents/文稿 - 陈曦的MacBook Air - 1/python data/实验6数据文件/data5.txt" 7 , "r" , encoding="utf-8") as f: 8 datas=f.readlines() 9 mem_num=random.sample(range(len(datas)),n) 10 for i in mem_num: 11 print(datas[i].strip()+"\n",end="") 12 13 with open("/Users/chenxi/Documents/文稿 - 陈曦的MacBook Air - 1/python data/实验6数据文件/{0}.txt".format(datetime.datetime.now().strftime('%Y%m%d')) 14 ,"w+",encoding="utf-8") as f: 15 for i in mem_num: 16 f.write(datas[i].strip()+"\n") 17 18 def task5_2(): 19 import random 20 import datetime 21 22 print(f"{'Roll call':=^50}") 23 24 while True: 25 n=eval(input("input roll call number:")) 26 if n==0: 27 break 28 29 with open("/Users/chenxi/Documents/文稿 - 陈曦的MacBook Air - 1/python data/实验6数据文件/data5.txt" 30 , "r" , encoding="utf-8") as f: 31 datas=f.readlines() 32 mem_num=random.sample(range(len(datas)),n) 33 for i in mem_num: 34 print(datas[i].strip()+"\n",end="") 35 36 with open("/Users/chenxi/Documents/文稿 - 陈曦的MacBook Air - 1/python data/实验6数据文件/{0}.txt".format(datetime.datetime.now().strftime('%Y%m%d')) 37 ,"w+",encoding="utf-8") as f: 38 for i in mem_num: 39 f.write(datas[i].strip()+"\n") 40 41 print(f"{'Over':=^50}")
运行截图
*
1.务必将数据处理干净,即使多费运行内存与时间!否则可能出现如图错误。
2.文件名处直接将函数作为参数代入,如实验4所写将文件名变量化将更清晰易读。