开发者

Read from source.sql write to destiantion.sql with python script?

I Have a file source.sql

INSERT INTO `Tbl_ABC` VALUES (1, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (2, 12, '3D STRUCTURES', '3D STRUCTURES', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (2, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (2, 111, '2D STRUCTURES', '3D STRUCTURES', NULL, NULL, 1)

I am going to write a new file called destination.sql.It will contains: The new file will ignore

`INSERT INTO `Tbl_ABC` VALUES (1, dont wirte if !=0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, don't write if !=0)

My sql may loger than this.but positon is keep in general. In this case the first number 0 at position[1] and the second 0 at the position[6] (start count from 开发者_运维问答0)

That the results should be like this .

INSERT INTO `Tbl_ABC` VALUES (1, 0, 'MMB', '2 MB INTERNATIONAL', NULL, NULL, 0)
INSERT INTO `Tbl_ABC` VALUES (2, 0, '2 STRUCTURES', '2D STRUCTURES', NULL, NULL, 0)

Anybody Here Could help me to find the way to format the source.sql file and write new file. destination.sql

Thanks ..


You can read line by line and check if it is ends with 0) and match with regex for the other one.

import re
dest=open("destination.sql","w+")
for line in open("source.sql","r"):
    if line.strip().endswith("0)") and re.search("\(\d+, 0,",line):
        dest.write(line)


Something like this:

import re

RX = re.compile(r'^.*?\(\d+,\s0,.*\s0\)\s*$')

outfile = open('destination.sql', 'w')
for ln in open('source.sql', 'r').xreadlines():
    if RX.match(ln):
        outfile.write(ln)


My sql may loger than this.but positon is keep in general. In this case the first number 0 at position[1] and the second 0 at the position[6] (start count from 0)

to S.Mark, fviktor I have tried like this

import re

RX = re.compile(r'^.*?\(\d+,\s0,.*\s0\)\s*$')

outfile = open('destination.sql', 'w')
for ln in open('source.sql', 'r').xreadlines():
    replace1 = ln.replace("INSERT INTO `Tbl_ABC` VALUES (", "")
    replace2 = replace1.replace(")", "")
    list_replace = replace2.split(',')
    #print list_replace
    #print '%s ,%s' % (list_replace[1], list_replace[6])
    if list_replace[6]==0 and list_replace[1] == 0:
        #start write line to destination.sql!!!!!!!! NEED HELP
        #if RX.match(ln):
        outfile.write(ln)

(2, x1, '2D STRUCTURES', '2D STRUCTURES', NULL, NULL, x6, 15, 2, '', NULL, NULL, NULL, NULL, '2D STRUCTURES', 'MAILLOT 12/08/05', -1, 'tata 20/05/02', 0, NULL, 0, NULL, NULL)

Of cause I don't need to write to destination.sql if the position[1] != 0 and position[6] != 0 in this case are x1 and x6. thanks for your

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜