将文件分对话拆分
1 f = open('D:\\record.txt') 2 3 boy = [] 4 girl = [] 5 count=1 6 7 for each_line in f: 8 9 if each_line[:3] != '===': 10 (role, line_spoken) = each_line.split(':',1) 11 if role == '小甲鱼': 12 boy.append(line_spoken) 13 if role == '小客服': 14 girl.append(line_spoken) 15 else: 16 17 boy_spoken_file_name = 'boy_'+str(count)+'.txt' 18 girl_spoken_file_name = 'girl_'+str(count)+'.txt' 19 20 21 boy_spoken = open('D:\\'+boy_spoken_file_name,'w') 22 girl_spoken = open('D:\\'+girl_spoken_file_name,'w') 23 24 boy_spoken.writelines(boy) 25 girl_spoken.writelines(girl) 26 27 boy_spoken.close() 28 girl_spoken.close() 29 30 boy = [] 31 girl = [] 32 33 count+=1 34 35 boy_spoken_file_name = 'boy_'+str(count)+'.txt' 36 girl_spoken_file_name = 'girl_'+str(count)+'.txt' 37 38 boy_spoken = open('D:\\'+boy_spoken_file_name,'w') 39 girl_spoken = open('D:\\'+girl_spoken_file_name,'w') 40 41 boy_spoken.writelines(boy) 42 girl_spoken.writelines(girl) 43 44 boy_spoken.close() 45 girl_spoken.close() 46 47 f.close()