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

实验五

作者:互联网

task3

with open('data3.txt','r',encoding='utf-8') as f :
    with open('data3_processed.txt', 'w', encoding='utf-8') as f1:
        data=f.read().strip().split('\n')
        data.pop(0)
        data1=[eval(i) for i in data]
        data2=[]
        for i in data1:
            if i - int(i) >= 0.5:
                data2.append(int(i) + 1)
            else:
                data2.append(int(i))
        data3=[int(i)for i in data2]
        f1.write(f'原始数据\t四舍五入后的数据\n')
        for j in range(len(data)):
            f1.write(f'{data1[j]}\t\t{data2[j]}\n')
print(f'原始数据:\n{data1}\n四舍五入后的数据:\n{data2}')

task4

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')

task5

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')

 

标签:f1,encoding,实验,print,txt,data1,data2
来源: https://www.cnblogs.com/iwangyuan/p/16278585.html