## 实验五 文件应用编辑

with open('data3.txt','r',encoding='utf-8') as f :
with open('data3_processed.txt', 'w', encoding='utf-8') as f1:
data.pop(0)
data1=[eval(i) for i in data]
data2=[]
for i in data1:
if i - int(i) >= 0.5:
data2.append(int(i) + 1)
else:
data2.append(int(i))
data3=[int(i)for i in data2]
f1.write(f'原始数据\t四舍五入后的数据\n')
for j in range(len(data)):
f1.write(f'{data1[j]}\t\t{data2[j]}\n')
print(f'原始数据:\n{data1}\n四舍五入后的数据:\n{data2}')

with open('data4.txt','r',encoding='utf-8')as f:
with open('data4_processed.txt','w',encoding='utf-8')as f1:
data1=[i.strip('\n').split('\t') for i in data]
x=data1.pop(0)
data2=[sorted(data1,key=lambda x: (x[2], -int(x[3])))]
print('\t\t'.join(x))
f1.write('\t\t'.join(x)+'\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 = 0
for i in data:
a += 1
print('行数：',a)

with open('data5.txt','r',encoding='utf-8')as f:
list2 = []
list1 = list(data)
list2 = data.split(' ')

b = 0
c = 0
for j in list1:
if j.isspace():
b += 1
c += 1
print('空格数',len(list2))
print('单词数',b)
print('字符数',c)

posted on 2022-05-17 11:42  haze13327620097  阅读(10)  评论(3编辑  收藏  举报