其他分享
首页 > 其他分享> > 实验5

实验5

作者:互联网

with open('data3.txt','r+',encoding='utf-8')as f:
    a=[]
    for i in f:
        i=i.strip('\n')
        a.append(i)
a.pop(0)
b=[eval(i) for i in a]
print(f'原始数据:\n{b}')
c=[round(eval(i)) for i in a]
print(f'四舍五入后数据:\n{c}')
with open('data3-1.txt','w',encoding='utf-8')as f:
    w1=['原始数据']+[str(i) for i in a]
    w2=['四舍五入后的数据']+[str(i) for i in c]
    for i in range(len(a)):
        f.write(f'{w1[i]}\t{w2[i]}\n')

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)

 

标签:encoding,data,data1,len,实验,txt,open
来源: https://www.cnblogs.com/ww88-666/p/16278605.html