你写eachline[0:6],后面只有5个=f = open('1.txt',encoding='utf-8')
me=[]
he=[]
count=1
for eachline in f:
if eachline[:5] != '=====':
role, linespoken=eachline.split(':')
if role=='我说':
me.append(linespoken)
if role=='他说':
he.append(linespoken)
#进行字符串的分割
else:
file_name_me = 'me_'+str(count)+'.txt'
file_name_he = 'he_'+str(count)+'.txt'
me_file=open(file_name_me,'w')
he_file=open(file_name_he,'w')
me_file.writelines(me)
he_file.writelines(he)
#文件的分别保存
me_file.close()
he_file.close()
me=[]
he=[]
count+=1
file_name_me = 'me_' + str(count) + '.txt'
file_name_he = 'he_' + str(count) + '.txt'
me_file = open(file_name_me, 'w')
he_file = open(file_name_he, 'w')
me_file.writelines(me)
he_file.writelines(he)
# 文件的分别保存
me_file.close()
he_file.close()
f.close()
|