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)

with open('data4.txt', 'r',encoding = 'utf-8') as f:
    data = f.read().split('\n')
for i in range(1,len(data)):
    data[i]=data[i].split('\t')
data1=data[1:].copy()
data1.sort(key=lambda x:(x[2],-int(x[3])),reverse = False)
data[0]+='\n'
for i in range(len(data1)):
    for j in range(len(data1[i])):
        if j==3:
            data1[i][j]+='\n'
        else:
            data1[i][j]+='\t'
    data[i+1]=''.join(data1[i])
with open('data6.txt', 'w',encoding = 'utf-8') as f:
    f.writelines(data) 
with open('data4.txt', 'r',encoding = 'utf-8') as f:
    data = f.read()
print(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)

 

posted on 2022-05-12 11:10  JskyR  阅读(28)  评论(0编辑  收藏  举报