# 实验五

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

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

with open('data5.txt','r',encoding='utf-8') as f:
x=len(a)
t=''.join(a)
kongge=t.count(' ')
danci=len(t.split())
zifu=len(t)
print(x)
print(danci)
print(kongge)
print(zifu)
with open('data5_with_line_number.txt','w',encoding='utf-8') as f:
for i in range(len(a)):
f.write(f'{i+1:<5}')
f.write(a[i])

posted @ 2022-05-16 22:20  天天三顿  阅读(6)  评论(3编辑  收藏  举报