# 实验五

with open('data3.txt','r+', encoding='UTF-8') as f:
x = [line.strip('\n') for line in f]
x.pop(0)
x_print = [eval(i) for i in x]
print(f"原始数据:\n{x_print}")
x_new = [round(eval(i)) for i in x]
print(f'四舍五入后的数据:\n{x_new}')

with open('data3_processed.txt','w') as f:
write1 = ['原始数据'] + [str(i) for i in x]
write2 = ['四舍五入后的数据'] + [str(i) for i in x_new]
for i in range(len(x)):
f.write(f"{write1[i]}\t{write2[i]}\n")

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

with open('data5.txt', 'r+', encoding='UTF-8') as f:
text = ''.join([i for i in f])
print(f'行数：{len(text.splitlines())}')
print(f'单词数：{len(text.split())}')
print(f"空格数：{text.count(' ')}")
print(f'字符数：{len(text)}')

with open('data5_with_line.txt', 'w', encoding='UTF-8') as f:
for i in range(len(text.splitlines())):
f.write('{} {}'.format(i+1,text.split('\n')[i]))
f.write('\n')

posted @ 2022-05-17 19:14  MeiKoY  阅读(7)  评论(2编辑  收藏  举报