实验5

task3

 1 with open('data3.txt','r+',encoding='utf-8')as f:
 2     a=[]
 3     for i in f:
 4         i=i.strip('\n')
 5         a.append(i)
 6 a.pop(0)
 7 b=[eval(i) for i in a]
 8 print(f'原始数据:\n{b}')
 9 c=[round(eval(i)) for i in a]
10 print(f'四舍五入后数据:\n{c}')
11 with open('data3-1.txt','w',encoding='utf-8')as f:
12     w1=['原始数据']+[str(i) for i in a]
13     w2=['四舍五入后的数据']+[str(i) for i in c]
14     for i in range(len(a)):
15         f.write(f'{w1[i]}\t{w2[i]}\n')

 

task4

 1 with open('data4.txt', 'r',encoding = 'utf-8') as f:
 2     data = f.read().split('\n')
 3 for i in range(1,len(data)):
 4     data[i]=data[i].split('\t')
 5 data1=data[1:].copy()
 6 data1.sort(key=lambda x:(x[2],-int(x[3])),reverse = False)
 7 data[0]+='\n'
 8 for i in range(len(data1)):
 9     for j in range(len(data1[i])):
10         if j==3:
11             data1[i][j]+='\n'
12         else:
13             data1[i][j]+='\t'
14     data[i+1]=''.join(data1[i])
15 with open('data6.txt', 'w',encoding = 'utf-8') as f:
16     f.writelines(data) 
17 with open('data4.txt', 'r',encoding = 'utf-8') as f:
18     data = f.read()
19 print(data)

 

task5

 1 data=[]
 2 word1=0
 3 word2=0
 4 word3=0
 5 with open('data5.txt', 'r',encoding = 'utf-8') as f:
 6     for i in f:
 7         data.append(i)
 8 for i in range(len(data)):
 9    word1+=len(data[i].split(' '))
10    word2+=len(data[i])
11    word3+=data[i].count(' ')
12 print('行数:',len(data))
13 print('单词数:',word1)
14 print('字符数:',word2)
15 print('空格数',word3)
16 for i in range(len(data)):
17     data[i]=str(i+1)+' '+data[i]
18 with open('data5.txt', 'w',encoding = 'utf-8') as f:
19      f.writelines(data)

 

posted on 2022-05-16 09:22  蒜头小兲  阅读(11)  评论(2编辑  收藏  举报

导航