1、新建一个名为"1.text"的文件,并写入一下内容。
小明:哈哈哈哈
小红:呵呵呵呵
小明:嘿嘿嘿嘿
小红:嘻嘻嘻嘻
==================================================
小明:飒飒飒飒
小红:走走走走
小明:坎坎坷坷
小红:啦啦啦啦
==================================================
小明:哈哈哈哈2
小红:呵呵呵呵2
小明:嘿嘿嘿嘿2
小红:嘻嘻嘻嘻2
==================================================
小明:飒飒飒飒2
小红:走走走走2
小明:坎坎坷坷2
小红:啦啦啦啦2
def saveFile(xm,xh,count):
file_name_xm='xm_'+str(count)+'.txt'
file_name_xh='xh_'+str(count)+'.txt'
xm_file=open(file_name_xm,'w')
xh_file=open(file_name_xh,'w')
xm_file.writelines(xm)
xh_file.writelines(xh)
f=open('D:\\aaapy\\1.txt')
xm = []
xh = []
count=1
for each_line in f:
if each_line[:8]!='========':
(role,line_spoken)=each_line.split(':')
if role=='小明':
xm.append(line_spoken)
if role =='小红':
xh.append(line_spoken)
else:
saveFile(xm,xh,count)
xm=[]
xh=[]
count+=1
saveFile(xm,xh,count)
f.close()