用python将SQL格式文件改成自己想要的格式

INSERT INTO TTT (NSAME, ID, AGE) VALUES ('AAA', '201023210816', '22');

将上面的SQL格式改成下面的格式

AAA|201023210816|22
import os

lists=[]
paths = os.getcwd()

for root,dirs,files in os.walk('wfyh'):
    for fn in files:
        lists.append(fn)



for nmb in lists:
    dfile=open("wfyh\\"+nmb,'r')
    nmbb=nmb.replace('.sql','')
    ofile=open("wf_test\\"+nmbb+'.txt','w')
    lists=[]
    for line in dfile:
        m=line.split('(')[2].replace(');','').replace(', ','|').replace('\'','')
        t = ''.join(m.split())
        lists.append(t)
    for linet in lists:
        ofile.write(linet+'\n')

dfile.close()
ofile.close()

将转换好格式的txt文件写入一个文件夹内

posted @ 2014-11-07 09:22  zjl-1110  阅读(501)  评论(0编辑  收藏  举报