hi 原因如下: #原始代码中bug比较多,已经逐一修正f = open('D:/tmp/record.txt')
boy = []
girl = []
count = 1
for each in f:
if each[:6] != '======': #应该是!=
(role, line_spoken) = each.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken) #拼写出错
if role == '小客服':
girl.append(line_spoken)
else:
boy_list = 'boy_' + str(count) + '.txt'
girl_list = 'gril_' + str(count) + '.txt'
r = open (boy_list, 'w')
k = open (girl_list, 'w')
r.writelines(boy)
k.writelines(girl) #拼写出错
r.close()
k.close()
boy = []
girl = [] #拼写错误
count += 1 #赋值出错
boy_list = 'boy_' + str(count) + '.txt' #如下代码需要加上,因为后面没有'======'分隔符
girl_list = 'gril_' + str(count) + '.txt'
r = open (boy_list, 'w')
k = open (girl_list, 'w')
r.writelines(boy)
k.writelines(girl)
r.close()
k.close()
boy = []
gril = []
f.close()
|