使用PYTHON消除sql文件中的分隔符(;,/)

时间:2017-09-15 12:03:12

标签: python

我有sql / text文件,我想检查分隔符,如&#34 ;;"," /"," GO"如果它们被重复或有它们的组合,那么它应该删除不需要的并且在输出文件中仅保留如下标准。

我写了下面的python程序,它做了一些工作,但某些情况下它失败了; /; /; /或/// ;;; (双重或重复终止子)。你对此的帮助非常赞赏。

import re

new_lines = re.compile('\n{2,50}')

# Read in the file
with open('file.sql', 'r') as file :
  filedata = file.read()
  filedata = re.sub(new_lines, '\r\n', filedata.strip())

# Replace the target string
for line in filedata:
   filedata = filedata.replace(';\n', '\n')
   filedata = filedata.replace('/\n', '\n')
   filedata.split('\n')
 #filedata = filedata.split('/')
 #filedata = re.sub(new_lines, '/', filedata.strip())
   filedata = re.sub(r'(/)\1+', r'\1', filedata)
   filedata = re.sub(r'(/)\1+', r'\1', filedata)

# Write the file out again
with open('fileOut.sql', 'w') as file:
  file.write(filedata)

输入文件1.sql:

-- Connect to Advantage PIP owner schema to run this script.
--ADVNY00125063
set echo on
set feedback on
set time on
set timing on

--Query to select the records where BUS_TYP_STA = 1 where cert_strt_dt = ‘12/31/9999’ and cert_end_dt is not null
SELECT
  TO_CHAR(CERT_STRT_DT,'DD-MM-YYYY'),
  TO_CHAR(DISP_CERT_STRT_DT,'DD-MM-YYYY'),
  TO_CHAR(CERT_END_DT,'DD-MM-YYYY'),
  VEND_CUST_CD
FROM
  R_VEND_BUS_TYP
WHERE
  BUS_TYP_STA    =1
AND CERT_STRT_DT =to_date('31-12-9999','DD-MM-YYYY')
AND CERT_END_DT IS NOT NULL;;//
//////////////
////////////
//////////
--Query to update the above selected records by setting CERT_STRT_DT and DISP_CERT_STRT_DT to be equal to the CERT_END_DT
UPDATE
  R_VEND_BUS_TYP
SET
  CERT_STRT_DT     =CERT_END_DT ,
  DISP_CERT_STRT_DT=CERT_END_DT
WHERE
  BUS_TYP_STA    =1
AND CERT_STRT_DT =to_date('31-12-9999','DD-MM-YYYY')
AND CERT_END_DT IS NOT NULL
///

COMMIT//
/

INSERT INTO DB_CHANGE_LOG VALUES ('ADVNY00125063 DML', SYSDATE)
/

COMMIT
/

输出文件2.sql:

-- Connect to Advantage PIP owner schema to run this script.
--ADVNY00125063
set echo on
set feedback on
set time on
set timing on

--Query to select the records where BUS_TYP_STA = 1 where cert_strt_dt = ‘12/31/9999’ and cert_end_dt is not null
SELECT
  TO_CHAR(CERT_STRT_DT,'DD-MM-YYYY'),
  TO_CHAR(DISP_CERT_STRT_DT,'DD-MM-YYYY'),
  TO_CHAR(CERT_END_DT,'DD-MM-YYYY'),
  VEND_CUST_CD
FROM
  R_VEND_BUS_TYP
WHERE
  BUS_TYP_STA    =1
AND CERT_STRT_DT =to_date('31-12-9999','DD-MM-YYYY')
AND CERT_END_DT IS NOT NULL
/

--Query to update the above selected records by setting CERT_STRT_DT and DISP_CERT_STRT_DT to be equal to the CERT_END_DT
UPDATE
  R_VEND_BUS_TYP
SET
  CERT_STRT_DT     =CERT_END_DT ,
  DISP_CERT_STRT_DT=CERT_END_DT
WHERE
  BUS_TYP_STA    =1
AND CERT_STRT_DT =to_date('31-12-9999','DD-MM-YYYY')
AND CERT_END_DT IS NOT NULL
/

COMMIT
/

INSERT INTO DB_CHANGE_LOG VALUES ('ADVNY00125063 DML', SYSDATE)
/

COMMIT
/

0 个答案:

没有答案