python30课拆分文本保存之后新文本内容是空的
f = open(r'e:\\record.txt', encoding = 'utf-8' )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 = 'girl' + str(count) + '.txt'
boy_file = open(file_name_boy,'w')
girl_file = open(file_name_girl,'w')
boy_file.writelines(boy)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
f.close() 你把写文件那两行改成以下代码试试?
boy_file = open(file_name_boy,'wb')
girl_file = open(file_name_girl,'wb') 已解决,留意':'和'======'都必须是英文的,包括txt文件。
页:
[1]