实验五

with open('data3.txt','r+', encoding='UTF-8') as f:
    Z=[]
    Y=[]
    x=[]
    for i in f:
        x.append(i.strip('\n'))
    newx=x[1:11]
    for i in newx:
        y=float(i)
        Y.append(y)
        if y-int(y) >= 0.5:
            y=int(y)+1
        else:
            y=int(y)
        Z.append(y)
    print(f'原始数据\n{Y}')
    print(f'四舍五入后的数据\n{Z}')

with open('data3_processed.txt','w',encoding='utf-8') as f:
    write1=['原始数据']+Y
    write2=['四舍五入后的数据']+Z
    for i in range (len(Y)):
        f.write(f'{write1[i]}\t{write2[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)

with open('data5.txt','r',encoding='utf-8')as f:
    with open('data5_with_line.txt', 'w', encoding='utf-8') as f1:
        a=f.read().strip('\n')
        b = len(a.splitlines())
        print('行数:', b)
        e = len(a.split())
        print('单词数:', e)
        c = 0
        for c, number in enumerate(a):
            c += 1
        print('字符数:', c)
        d = 0
        for i in a:
            if i == ' ':
                d += 1
        print('空格数:', d)
        a=a.split('\n')
        n='\n'.join(a)

        for i in range(len(a)):
            f1.write(f'{i+1}.{a[i]}\n')

 

posted @ 2022-05-17 15:28  朗格里格朗  阅读(5)  评论(2编辑  收藏  举报