实验五

with open('data3.txt', 'r', encoding = 'utf-8') as f:
    data2= [line.strip('\n') for line in f]

    num=[eval(i) for i in data2[1:]]
    print(f'原始数据是:\n{num}')
    num1=[round(i) for i in num]
    print(f'四舍五入后的数据是:\n{num1}')
    with open('data3_processed.txt','w',encoding = 'utf-8') as m:
        x=[['原始数据是']+[str(i) for i in num]]
        y=[['四舍五入后的数据']+[str(i) for i in num1]]
        for i in range(len(num)):
            m.write(f"{x[i]}\t{y[i]}\n")

with open('data4.txt', 'r+', encoding='UTF-8') as f:
    string = [i.strip('\n').split('\t') for i in f]
    a = string.pop(0)
    string.sort(key=lambda x: (x[2], -int(x[-1])))
    print('\t'.join(a))
    for i in string:
        print('\t'.join(i))

with open('data4_processed.txt', 'w') as f :
    f.write('\t'.join(a)+'\n')
    for i in string:
        f.write('\t'.join(i)+'\n')
        data.sort(key=lambda x: (x[2], -int(x[3])))

with open('data5.txt', 'r', encoding = 'utf-8') as f:
    data=''.join([i for i in f])
    print(data)
    space_counts = 0
    zfs=0
    with open('data5_with_line.txt', 'w', encoding='utf-8') as m:
        for i in range(len(data)):
            for x in data[i]:
                if x == ' ':
                    space_counts += 1
                else:
                    space_counts = space_counts
            m.write(f'{i + 1} {data[i]}\n')
print(f'''字符数:{len(data)}
单词数:{len(data.split())}
空格数:{space_counts}
行数:{len(data.splitlines())}''')

 

posted @ 2022-05-13 19:56  苏萌0017  阅读(12)  评论(2编辑  收藏  举报