实验五

with open('data3.txt','r+', encoding='UTF-8') as f:
num = [line.strip('\n') for line in f]
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}')

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

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

with open('data5.txt','r',encoding='utf-8')as f:
with open('data5_with_line.txt', 'w', encoding='utf-8') as f1:
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 10:21  一个幽灵  阅读(5)  评论(1编辑  收藏  举报