美文网首页
python 文件读写练习

python 文件读写练习

作者: 陈智涛 | 来源:发表于2017-10-23 15:48 被阅读0次
#!/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')

相关文章

网友评论

      本文标题:python 文件读写练习

      本文链接:https://www.haomeiwen.com/subject/ckrauxtx.html