|
|
发表于 2020-6-2 22:26:57
|
显示全部楼层
本楼为最佳答案
错误地方 if 条件这 切片要切 6 个 等于号 if each_line[:6] != '======'
完整代码:
- f = open('F:\\record.txt')
- boy = []
- girl = []
- count = 1
- 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:
- # 文件的分别保存操作
- file_name_boy = 'boy_' + str(count) + '.txt'
- file_name_girl = 'firl_' + str(count) + '.txt'
- boy_file = open(file_name_boy, 'w')
- firl_file = open(file_name_girl, 'w')
- boy_file.writelines(boy)
- firl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- boy = []
- firl = []
- count += 1
- f.colse()
复制代码 |
|