1 #coding:utf-8  
 2 '''''cdays-4-exercise-6.py 文件基本操作 
 3     @note: 文件读取写入, 列表排序, 字符串操作 
 4     @see: 字符串各方法可参考hekp(str)或Python在线文档http://docs.python.org/lib/string-methods.html 
 5 '''  
 6   
 7 f = open('cdays-4-test.txt', 'r')                   #以读方式打开文件  
 8 result = list()  
 9 for line in f.readlines():                          #依次读取每行  
10     line = line.strip()                             #去掉每行头尾空白  
11     if not len(line) or line.startswith('#'):       #判断是否是空行或注释行  
12         continue                                    #是的话,跳过不处理  
13     result.append(line)                             #保存  
14 result.sort()                                       #排序结果  
15 print result  
16 open('cdays-4-result.txt', 'w').write('%s' % '\n'.join(result)) #保存入结果文件

 

相关文章:

  • 2021-07-06
  • 2021-11-17
  • 2022-02-19
  • 2021-12-14
  • 2022-12-23
  • 2021-11-27
  • 2021-07-17
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2022-01-24
  • 2021-10-07
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案