实验五

1 with open('data3.txt','r',encoding='utf-8')as f:
2     data=f.read().strip().split('\n')
3 data[0]='原始数据'+'\t'+'四舍五入后数据'+'\n'
4 for i in range(1,len(data)):
5     data[i]=data[i] + '\t' + str(int(eval(data[i])+0.4))+'\n'
6 with open('data3out.txt','a',encoding='utf-8')as f:
7     f.writelines(data)

 1 with open('data4.txt','r',encoding='utf-8') as f:
 2     yuan=f.read().split('\n')
 3     data=['']*(len(yuan)-1)
 4     res=['']*len(yuan)
 5     for i in range(0,len(yuan)):
 6         yuan[i]=list(yuan[i].split('\t'))
 7         if i>0:
 8             yuan[i][3]=int(yuan[i][3])
 9             data[i-1]=yuan[i]
10     data.sort(key=lambda s:(s[2],-s[3]))
11     for j in range(0, len(yuan[0])):
12         res[0] += str(yuan[0][j]) + '\t'
13     res[0] += '\n'
14     for i in range(0,len(data)):
15         for j in range(0, len(data[i])):
16             res[i+1] += str(data[i][j]) + '\t'
17         res[i+1] += '\n'
18 with open('data4out.txt','a',encoding='utf-8') as f:
19     f.writelines(res)

 1 data=[]
 2 s1=0
 3 s2=0
 4 s3=0
 5 with open('data5.txt', 'r',encoding = 'utf-8') as f:
 6     data=f.readlines()
 7 for i in range(len(data)):
 8    s1+=len(data[i].split(' '))
 9    s2+=len(data[i])
10    s3+=data[i].count(' ')
11 print('行数:',len(data));print('单词数:',s1)
12 print('字符数:',s2);print('空格数',s3)
13 for i in range(len(data)):
14     data[i]=str(i+1)+' '+data[i]
15 with open('data5out.txt', 'w',encoding = 'utf-8') as f:
16      f.writelines(data)

 

posted @ 2022-05-15 21:54  张博睿  阅读(6)  评论(1编辑  收藏  举报