Если размер дампа небольшой:
rez = open("mysqldatadump.txt").read().split("-- Current Database:") for x in rez[1:]: fn= x.split('\n')[0].replace('`',"").strip() open("%s.sql" % fn,"w").write("-- %s" % x)
Если файл очень большой:
#! /usr/bin/python # -*- coding: utf-8 -*- inputFile="d:/md.sql" outputDir="d:/parseddump" current_out=None if not glob.glob(outputDir): os.mkdir(outputDir) t=time.time() for row in open(inputFile): if "-- Current Database: `" in row: cudir="%s\\%s" % (outputDir,row.split("`")[1].split("`")[0]) print cudir if not glob.glob(cudir): os.mkdir(cudir) if "-- Table structure for table `" in row: if current_out: current_out.close() current_out=open("%s/%s.sql" % (cudir, row.split("`")[1].split("`")[0]), "w") if current_out: current_out.write(row) if current_out: current_out.close() print time.time()-t