# 实验5

with open('data3.txt','r',encoding= 'utf-8') as f:
d = [eval(line) for line in data[1:]]
print('原始数据: ')
print(d)
t = []
for i in d:
x = float(i)
y = int(x)
if x-y >= 0.5:
y += 1
t.append(y)
else:
y = y
t.append(y)
print('四舍五入后数据: ')
print(t)
s = ['原始数据\t四舍五入后数据\t\n']
for a,b in zip(d,t):
s.append(str(a)+'\t'+'          '+str(b)+'\n')
with open('data3_processed.txt','w',encoding='utf-8') as f:
f.write(''.join(s))

with open('data4.txt','r',encoding='utf-8') as f:
with open('data4_processed.txt', 'w', encoding='utf-8') as f1:
d = [i.strip('\n').split('\t') for i in data]
t = d.pop(0)
data2 = [sorted(d,key=lambda t: (t[2], -int(t[3])))]
print('\t\t'.join(t))
f1.write('\t\t'.join(t) + '\n')
for i in data2:
for j in i:
print('\t'.join(j))
f1.write('\t'.join(j)+'\n')

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

posted on 2022-05-15 15:28  好运来。  阅读(10)  评论(2编辑  收藏  举报