文件拆分

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37416991/article/details/81233974
#/usr/bin/python3
#-*- coding:utf-8 -*-
import re
f = open('1.txt') #打开要拆分文件
yueke = []
heimao = []
count = 1

def write_file(): #创建拆出的文件
    file_name_yueke = 'yueke' + str(count)+'a.txt'
    file_name_heimao = '黑猫'+str(count)+'a.txt'

    yueke_file = open(file_name_yueke,'w')
    heimao_file = open(file_name_heimao ,'w')

    yueke_file.writelines(yueke)
    heimao_file.writelines(heimao)
    yueke_file.close()
    heimao_file.close()
for each_line in f: #读取拆分文件
    if each_line[:6] != "======":
        if re.split(':',each_line)[0]=='yueke': #拆分规则设定
            yueke.append(re.split(':',each_line)[1])
        if re.split(':',each_line)[0]=='黑猫':
            heimao.append(re.split(':',each_line)[1])
    else:
        write_file() 
        yueke = []
        heimao = []
        count += 1
write_file()
f.close()

猜你喜欢

转载自blog.csdn.net/m0_37416991/article/details/81233974