|  | 
 
| 
import pickle
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 def save_file(boy,girl,count):
 file_name_boy = 'boy_' + str(count) + '.txt'
 file_name_girl = 'girl_' + str(count) + '.txt'
 
 boy_file = open(file_name_boy,'wb')
 girl_file = open(file_name_girl,'wb')
 
 pickle.dump(boy,boy_file)
 pickle.dump(girl,girl_file)
 
 boy_file.close()
 girl_file.close()
 
 def split_file(file_name):
 count = 1
 
 boy = []
 girl = []
 
 f = open(file_name)
 
 for each_line in f:
 if each_line[:6] != '======':
 (role,line_spoken) == each_line.split(':',1)
 if role == '小甲鱼':
 boy.append(line_spoken)
 if role == '小客服':
 girl.append(line_spoken)
 else:
 save_file(boy,girl,count)
 
 boy = []
 girl = []
 count += 1
 
 save_file(boy,girl,count)
 f.close()
 
 split_file('record.txt')
 
 
 
 
 
 
西麦 发表于 2018-5-5 21:46for循环外的save_file(boy,girl,count)
 是说如果没有检测到‘======’时,执行这个save_file(boy,girl,c ...
里面是用append不断将数据添加到列表中,只要不是全等号的就都存放好,最后用该语句写到文件中 | 
 |