实验5 文件应用编程

实验任务3

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')     

实验任务4

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())

实验任务5

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