简体   繁体   中英

Python replace string inside file itself using regular expression

I'm developing on a RedHat Linux server and the Python version installed is the 2.6 . I have a Python script that have to replace a string in a file .sql . Each time I run the Python script I pass a different argument that must replace the oldest value inside the same .sql file.

File .sql has a content like this:

SELECT * FROM MY_TABLE WHERE 1=1
AND '1234567890' BETWEEN START_VALUE AND END_VALUE
AND JOB = 'EMPLOYEE';

I don't want to use some temporary files for the elaboration, I should have to read the .sql and modify it contextually.

I searched on other posts but I've never found the final solution. I tried to use re.sub but I didn't find how to make the replacement in effect inside .sql .

I have no idea if this works for 2.6 - sorry. I don't no anything about RedHat Server either - sorry. And I don't know much about SQL - sorry. This isn't going well right? :o) But if this helps as an example...

Given a test.sql file containing...

DECLARE 
@START_VALUE = 100
@END_VALUE = 200
SELECT * FROM MY_TABLE WHERE 1=1
AND '1234567890' BETWEEN START_VALUE AND END_VALUE
AND JOB = 'EMPLOYEE';

You could try...

import re

# Read data from file
with open('test.sql', 'r') as file:
    data = file.read()

print(data)

data = re.sub(r'@END_VALUE = (\d+)', r'@END_VALUE = 555', data)
print()
print('After the change...')
print()
print(data)

# Write data back 
with open('test.sql', 'w') as file:
    file.write(data)

Outputs:

DECLARE 
@START_VALUE = 100
@END_VALUE = 200
SELECT * FROM MY_TABLE WHERE 1=1
AND '1234567890' BETWEEN START_VALUE AND END_VALUE
AND JOB = 'EMPLOYEE';

After the change...

DECLARE 
@START_VALUE = 100
@END_VALUE = 555
SELECT * FROM MY_TABLE WHERE 1=1
AND '1234567890' BETWEEN START_VALUE AND END_VALUE
AND JOB = 'EMPLOYEE';

Note above that the END_VALUE changes from '200' to '555'. If you try it and it gets errors maybe look at the part erroring and find the equivalent way of doing it in Python 2.x etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM