实验5 文件应用编程

3.实验任务3

 

f=open('data3.txt','r') g=open('data3_processed.txt','w') a=[line.strip('\n') for line in f] a.pop(0) list1=[eval(j) for j in a] print(f'原始数据:\n{list1}') list2=[round(i) for i in list1] print(f'四舍五入后数据:\n{list2}') c=['原始数据']+[i for i in list1] d=['四舍五入后数据']+[i for i in list2] for i in range(len(list1)):     g.write(f{c[i]}\t\t{d[i]}\n) f.close() g.close()

 

4.实验任务4

 

f=open('data4.txt','r') g=open('data4_processed.txt','w') num=[line.strip('\n') for line in f] fenshu=[i.split('\t') for i in num] num_=fenshu.pop(0) fenshu.sort(key=lambda x:(x[2], x[3]),reverse=True) g.write('\t'.join(num_)) g.write('\n') for i in fenshu:     g.write('\t'.join(i))     g.write('\n') f.close() g.close() t=open(data4_processed.txt,encoding=utf-8) print(t.read()) t.close()

5.实验任务5

 

num=0 g=open('data5_with_line.txt','w') f=open('data5.txt','r') while True:     a=f.readline()     if not a:         break     num+=1     print(num,a,end='')     g.write(str(num))     g.write(' ')     g.write(a)     g.write('\n') f.close() g.close()

 实验总结:参考了一下别人的代码,还是不太懂QAQ