python 文件读写练习

#!/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')

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容