实验5

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

data=[]
word1=0
word2=0
word3=0
with open('data5.txt','r',encoding='utf_8')as f:
    for i in f:
        data.append(i)
for i in range(len(data)):
    word1+=len(data[i].split(' '))
    word2+=len(data[i])
    word3=data[i].count(' ')
print('行数',len(data))
print('单词数',word1)
print('字符数',word2)
print('空格数',word3)
for i in range(len(data)):
    data[i]=str(i+1)+' '+data[i]
with open('data5.txt','w',encoding='utf_8')as f:
    f.writelines(data)

 
with open('data4.txt','r',encoding='utf-8')as f:
      with open('data4_processed.txt','w',encoding='utf-8')as f1:
          data=f.readlines()
          data1=[i.strip('\n').split('\t') for i in data]
          x=data1.pop(0)
          data2=[sorted(data1,key=lambda x: (x[2], -int(x[3])))]
          print('\t\t'.join(x))
          f1.write('\t\t'.join(x)+'\n')
          for i in data2:
             for j in i:
                 print('\t'.join(j))
                 f1.write('\t'.join(j)+'\n')


 

posted on 2022-05-16 23:06  唔a  阅读(6)  评论(2编辑  收藏  举报

导航