一、实验结论:
1.实验任务1-5:已自行练习
2.实验任务6:task6.py
程序源码:
1 with open(r'C:\Users\28678\Downloads\实验5数据文件\data6.csv','r', 2 encoding='gbk') as f: 3 data=f.readlines() 4 5 data.remove('原始数据\n') 6 data0=[] 7 for i in data: 8 if i[-1]=='\n': 9 i=i[:-1] 10 data0.append(float(i)) 11 print('原始数据:') 12 print(data0) 13 14 data1=[int(i+0.5) for i in data0] 15 print('四舍五入后数据:') 16 print(data1) 17 18 with open(r'C:\Users\28678\Downloads\实验5数据文件\data6_processed.csv','w', 19 encoding='gbk') as f: 20 f.write('原始数据,四舍五入后数据\n') 21 for i in range(len(data1)): 22 f.write(str(data0[i])+','+str(data1[i])+'\n')
运行结果截图:
3.实验任务7:task7.py
程序源码:
1 with open(r'C:\Users\28678\Downloads\实验5数据文件\data7.csv','r', 2 encoding='gbk') as f: 3 data=f.readlines() 4 5 data0=[i.strip('\n').split(',') for i in data] 6 data0.remove(data0[0]) 7 8 data1=sorted(data0,key=lambda x:(x[2],-int(x[3]))) 9 10 print(f'{"学号":10s}{"姓名":10s}{"专业":10s}{"分数":10s}') 11 for h,i,j,k in data1: 12 print(f'{h:10s}{i:10s}{j:10s}{k:10s}') 13 14 with open(r'C:\Users\28678\Downloads\实验5数据文件\data7_processed.csv','w', 15 encoding='gbk') as f: 16 f.write('学号,姓名,专业,分数\n') 17 for i in range(len(data1)): 18 f.write(','.join(data1[i])+'\n')
运行结果截图:
4.实验任务8:task8.py
程序源码:
1 with open(r'C:\Users\28678\Downloads\实验5数据文件\hamlet.txt','r') as f: 2 data=f.read() 3 4 print('hamlet.txt粗略统计:') 5 print(f'行数:{len(data.splitlines())}') 6 print(f'单词数:{len(data.split())}') 7 print(f'字符数:{len(data)}') 8 print(f'空格数:{data.count(" ")}') 9 10 data1=data.splitlines() 11 data2=[] 12 for i in range(len(data1)): 13 data2.append(f'{i+1} '+data1[i]+'\n') 14 15 with open(r'C:\Users\28678\Downloads\实验5数据文件\hamlet_with_line_number.txt', 16 'w') as f: 17 f.writelines(data2)
运行结果截图:
5.实验任务9:task9.py
程序源码:
1 def is_valid(id): 2 if len(id)==18 and id[:-1].isdigit() and (id[-1].isdigit() or id[-1]=='X'): 3 return True 4 else: 5 return False 6 7 with open(r'C:\Users\28678\Downloads\实验5数据文件\data9_id.txt','r') as f: 8 data=f.readlines() 9 10 data.remove(data[0]) 11 data0=[i.strip('\n').split(',') for i in data] 12 13 data1=[] 14 for i in data0: 15 if is_valid(i[1])==True: 16 i[1]=i[1][6:14] 17 data1.append(i) 18 19 import datetime 20 t=datetime.datetime.now() 21 y=t.strftime('%Y%m%d') 22 for i in data1: 23 i.append(str(int(y)-int(i[1][0:8]))) 24 25 data2=sorted(data1,key=lambda x:-int(x[2])) 26 27 print('姓名, 出生日期, 年龄') 28 for i in data2: 29 i[1]=i[1][0:4]+'-'+i[1][4:6]+'-'+i[1][6:8] 30 i[2]=i[2][0:2] 31 print(', '.join(i))
运行结果截图:
6.实验任务10:task10_1.py
程序源码:
1 with open(r'C:\Users\28678\Downloads\实验5数据文件\data10_stu.txt','r') as f: 2 data=f.readlines() 3 4 data0=[i.strip('\n').split('\t') for i in data] 5 6 import random 7 n=eval(input('输入随机抽点人数:')) 8 data1=random.sample(data0,n) 9 for i in data1: 10 print(' '.join(i)) 11 12 import datetime 13 t=datetime.datetime.now() 14 fn=t.strftime('%Y%m%d') 15 16 with open(r'C:\Users\28678\Downloads\实验5数据文件\{}.txt'.format(fn),'w') as f: 17 for i in data1: 18 i[0]+='\t' 19 i[1]+='\t' 20 i[2]+='\n' 21 f.writelines(i)
运行结果截图:
task10_2.py
程序源码:
1 with open(r'C:\Users\28678\Downloads\实验5数据文件\data10_stu.txt','r') as f: 2 data=f.readlines() 3 4 data0=[i.strip('\n').split('\t') for i in data] 5 6 import datetime 7 t=datetime.datetime.now() 8 fn=t.strftime('%Y%m%d') 9 10 import random 11 print(f'{"抽点开始":*^32s}') 12 n=eval(input('输入随机抽点人数:')) 13 a=0 14 while n!=0: 15 data1=random.sample(data0,n) 16 a+=1 17 for i in data1: 18 print(' '.join(i)) 19 data0.remove(i) 20 if a==1: 21 with open(r'C:\Users\28678\Downloads\实验5数据文件\{}.txt'.format(fn),'w') as f: 22 for i in data1: 23 i[0]+='\t' 24 i[1]+='\t' 25 i[2]+='\n' 26 f.writelines(i) 27 else: 28 with open(r'C:\Users\28678\Downloads\实验5数据文件\{}.txt'.format(fn),'a') as f: 29 for i in data1: 30 i[0]+='\t' 31 i[1]+='\t' 32 i[2]+='\n' 33 f.writelines(i) 34 n=eval(input('输入随机抽点人数:')) 35 print(f'{"抽点结束":*^32s}')
运行结果截图:
二、实验总结:
1. 能够在代码中正确表示文件的路径 2. 能灵活、正确使用python内置的文件读写基础操作:打开/关闭、读写、遍历,会读写csv格式文件 3. 复习字符串、列表、lambda匿名函数和sorted函数的用法,以及不同类型之间相互转换 4. 熟悉random模块、datetime模块 5. 针对具体问题场景,能够用文件和组合数据类型编程解决应用问题标签:文件,open,data,Downloads,编程,实验,print,data1,data0 From: https://www.cnblogs.com/myf-/p/17444625.html