1 with open('data3.txt', 'r', encoding='utf-8') as f:
2     data = f.read().strip().split('\n')
3 data[0] = '原始数据' + '\t' + '四舍五入后数据' + '\n'
4 for i in range(1, len(data)):
5     data[i] = data[i] + '\t\t' + str(int(eval(data[i]) + 0.4)) + '\n'
6 with open('data3.txt', 'w', encoding='utf-8') as f:
7     f.writelines(data)

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

 1 with open('data5.txt', 'r', encoding='utf-8') as f:
 2     data = f.read().strip('\n')
 3     a = data.split('\n')
 4     b = '\n'.join(a)
 5     space_counts = 0
 6     with open('data5_with_line.txt', 'w', encoding='utf-8') as g:
 7         for i in range(len(a)):
 8             for x in a[i]:
 9                 if x == ' ':
10                     space_counts += 1
11                 else:
12                     space_counts = space_counts
13             g.write(f'{i+1} {a[i]}\n')
14 print(f'''行数:{len(a)}
15 单词数:{len(b.split())}
16 空格数:{space_counts}
17 字符数:{len(b)}''')

 

posted on 2022-05-16 12:26  starry。  阅读(6)  评论(2编辑  收藏  举报