#!/usr/bin/python
#对文件进行读写操作练习,将陈智涛和客服的对话分开,分别存到customer和service文件>中,并且遇到========,就对文件加1,重新写入
def fileSave(filename,content,count):
filename = filename+'_'+str(count)+'.txt'
f = open(filename,'w')
f.writelines(content)
f.close()
def splitFile(filename):
f = open('record.txt')
customer = []
service = []
count = 1
for line in f:
if line[:6] != '======' and line.strip() != '':
(role,record) = line.split(':',1)
if '陈智涛'== role:
customer.append(record)
if '客服' == role:
service.append(record)
else:
fileSave('customer',customer,count)
fileSave('service',service,count)
customer = []
service = []
count +=1
fileSave('customer',customer,count)
fileSave('service',service,count)
f.close()
splitFile('record.txt')
网友评论