脚本宝典收集整理的这篇文章主要介绍了实验5 文件应用编程,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
with open('data3.txt','r',encoding='utf-8') as f: be=[] b=f.readlines() for i in b[1::]: i=eval(i.strip('n')) be.append(i) print(f'原始数据:n{be}') a=[] for i in be: if i%1>=0.5: i=int(i)+1 a.append(i) else: i=int(i) a.append(i) print(f'四舍五入后数据:n{a}') with open('data3.processeed.txt','w') as f: be1=['原始数据'] be2=be1+be a1=['四舍五入后数据'] a2=a1+a for i in range(len(be)+1): f.write(str(be2[i])+'t'+str(a2[i])+'n')
with open('data4.txt','r') as f: list1=f.readlines() b=[] major=[] score=[] name=list1[0] list1.remove(name) for i in list1: i=i.split('t') c=i[2] major.append(c) a=i[0]+'t'+i[1] d=i[3] score.append(d) b.append(a) list2=sorted(list(zip(major,b,score))) list3=[list(i) for i in list2[0:7]] list4=[i[::-1] for i in list3] list5=sorted(list4,reverse=True) list6=[list(i) for i in list2[8::]] list7=[i[::-1] for i in list6] list8=sorted(list7,reverse=True) lista=list5+list8 tir1=[] tir2=[] tir3=[] for j in lista: tir1.append(j[0]) tir2.append(j[1]) tir3.append(j[2]) with open('data4_processed.txt','w') as f: f.writelines(name) for i in range(len(tir1)): f.write(tir2[i]+'t'+tir3[i]+'t'+tir1[i]+'n') with open('data4_processed.txt','r') as f: print(f.read())
with open('data5.txt','r') as f: b=f.readlines() with open('data5.txt','r')as f: a=f.read() kong=a.count(' ') words=a.split() print(f'行数:{len(b)}') print(f'单词数:{len(words)}') print(f'空格数:{kong}') print(f'字符数:{len(a)}') with open('data5_with_line.txt','w') as f: x=1 for i in b: i=str(x)+' '+i f.write(i) x+=1
以上是脚本宝典为你收集整理的实验5 文件应用编程全部内容,希望文章能够帮你解决实验5 文件应用编程所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。