实验五

 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')

 1 with open('data4.txt','r+',encoding='utf-8')as f:
 2     a=[i.strip('\n').split('\t') for i in f]
 3     b=a.pop(0)
 4     a.sort(key=lambda x:(x[2],-int(x[-1])))
 5     print('\t'.join(b))
 6     for i in a:
 7         print('\t'.join(i))
 8 with open('data4.1.txt','w',encoding='utf-8')as f:
 9     f.write('\t'.join(b)+'\n')
10     for i in a:
11         f.write('\t'.join(i)+'\n')

 1 with open('data5.txt', 'r', encoding='UTF-8') as f:
 2     text = ''.join([i for i in f])
 3     print(f'行数:{len(text.splitlines())}')
 4     print(f'单词数:{len(text.split())}')
 5     print(f"空格数:{text.count(' ')}")
 6     print(f'字符数:{len(text)}')
 7 
 8 with open('data5.txt', 'r', encoding='UTF-8') as f:
 9     lines=f.readlines()
10     ls=[str(i+1)+' '+line for i,line in enumerate(lines)]
11     new_content=''.join(ls)
12     print(new_content)
13 with open('data5.1.txt','w',encoding='utf-8')as f:
14     f.write(new_content)

实验总结:每一个代码都可以有多种不同的实现方式,但是核心思想不变,只要掌握核心思想,接下来就是怎样操作的问题。

也许某一个实验有更简便的方法,但是最重要的是自己去探索!

 

posted @ 2022-05-15 22:19  爱冒险的小洋  阅读(12)  评论(2编辑  收藏  举报