将指定格式的文章进行保存

文件系统的相关知识点:

采用函数形式可以缩减工作,或这泡菜函数均可。

#! -*- coding:utf-8 -*-
#Author:sjf
#QQ:1186777415
#Version1.1
def save_file(xiong , wang , count ):    
      file_name_xiong = 'xiong' + str(count) + '.txt'    
      file_name_wang = 'wang' + str(count) + '.txt'    
xiong_file = open(file_name_xiong, 'w')    
wang_file = open(file_name_wang, 'w')    
xiong_file.writelines(xiong)    
wang_file.writelines(wang)    
xiong_file.close()    
wang_file.close()f = open('C:\\Users\\sjf\\Desktop\\haha.txt')
xiong  =  []
wang =  []
count = 1
for each_line in f :    
      if each_line[:6] != '======':        
      ( role,line_spoken) = each_line.split(':', 1 )       
               if role == '熊':            
               xiong.append(line_spoken)       
               else:            
               wang.append(line_spoken)    
      else:       
      save_file(xiong,wang, count)        
      xiong = []        
      wang = []       
      count += 1

save_file(xiong, wang, count)
f.close()

f.close()

你可能感兴趣的:(将指定格式的文章进行保存)