# 实验5

task3.
mport math
f1=open('D:\\data3.txt','r')
list1,list2=[],[]
for line in f1.readlines():
line=line.strip('\n')

list1.append(line)
print(list1[0])
del list1[0]
print(list1)
for i in list1:
i=float(i)
if i +0.5>=int(i)+1:
i=math.ceil(i)
else:
i=int(i)
list2.append(i)
f2=open('D:\\data3.new.txt','w')
f2.write('原始数据:\t四舍五入数据:\n')
for i in range(len(list1)):
f2.write(f'{list1[i]}\t{list2[i]}\n')
f2.close()
print(f'四舍五入数据:\n{list2}')

f1=open('D:\\data4.txt','r')
list1=[]
for line in f1.readlines():
line=line.split()
list1.append(line)
b=list1[0];del list1[0]
list1.sort(key=lambda x : (x[2],-int(x[3])))
list1.insert(0,b)
f2=open('D:\\data4_processed.txt','w')
task。4
list3=[]
for i in list1:
list2=[]
for x in i:
if x!=i[-1]:
y=x+'\t'
else:
y=x+'\n'
list2.append(y)

list3=list3+list2
for i in range(len(list3)):
f2.writelines(list3[i])
f2.close()
for i in list3:
for x in i:
print(x,end='')

task.5
f1=open('D:\\data5.txt','r')
list1,list2,list3=[],[],[]

sum=0
for s in f1.read():
list1.append(s)
f1.close()
dic={'行数:':0,'单词数:':0,'空格数:':0,'字符数:':0}
for i in list1:
if i==' ':
dic['空格数:']+=1
dic['字符数:']=len(list1);
f1=open('D:\\data5.txt','r')

for i in f1.readlines():
list3.append(i)
list2.append(i.split())
dic['行数:']=len(list3)
for i in list2:
dic['单词数:']+=len(i)
for i,x in dic.items():
print(i,x)
f2=open('D:\\data5_with_line.txt','w')
i=1
for i in range(len(list3)):
f2.writelines(str(i+1)+'\t'+list3[i])
f2.close()

posted @ 2022-05-15 16:47  李禹灼  阅读(12)  评论(3编辑  收藏  举报