实验五

with open('data5.txt','r',encoding='utf-8')as f:
    data = f.read()
 
line_num = len(data.splitlines())
word_num = len(data.split())
space_num = data.count(' ')
totall_num = len(data)
print(f'行数:{line_num}\n单词数:{word_num}\n空格数:{space_num}\n字符数:{totall_num}\n')
 
text = data.splitlines()
content = []
n = 1
for i in text:
    content.append(f'{n}.{i}\n')
    n+=1
 
# print(content)
for i in content:
    print(i)
 
with open('data5_with_line.txt', 'x', encoding='utf-8') as f:
    f.writelines(i for i in content)

with open('data4.txt','r',encoding='utf-8')as f:
    data = f.readlines()
    massage = [i.strip('\n').split('\t') for i in data]
    a = massage.pop(0)
    massage.sort(key=lambda x:(x[2],-int(x[3])))
    print('\t'.join(a))
    for i in massage:
        print('\t'.join(i))
 
with open('data4_processed.txt', 'x') as f :
    f.write('\t'.join(a)+'\n')
    for i in massage:
        f.write('\t'.join(i)+'\n')

with open('data3.txt','r',encoding='utf-8')as f:
    data = f.readlines()
    prossed = open('data3_processed.txt','x',encoding='utf-8')
    num = [line.strip('\n') for line in data]
    num.pop(0)
    num_print = [eval(i) for i in num]
    print(f'原始数据:\n{num_print}')
    num_new = [round(eval(i)) for i in num]
    print(f'四舍五入后的数据:\n{num_new}')
    w1 = ['原始数据'] + [str(i) for i in num]
    w2 = ['四舍五入后的数据'] + [str(i) for i in num_new]
    f.seek(0)
    for i in range(len(num_new)):
        prossed.write(f'{w1[i]}\t{w2[i]}')

 

posted @ 2022-05-15 15:54  我真是想笑  阅读(8)  评论(1编辑  收藏  举报