Существует множество тем о поиске и замене текста для python, но я думаю, что мой вопрос другой.
У меня есть куча java-файлов с
System.out.println("some text here");
Я пытаюсь написатьскрипт Python, который заменит их все на
if (logger.isInfoEnabled()) {
logger.info("some text here");
}
. Для этого я попытался:
def findReplace(fileName, sourceText, replaceText):
file = open(fileName, "r") #Opens the file in read-mode
text = file.read() #Reads the file and assigns the value to a variable
file.close() #Closes the file (read session)
file = open(fileName, "w") #Opens the file again, this time in write-mode
file.write(text.replace(sourceText, replaceText)) #replaces all instances of our keyword
# and writes the whole output when done, wiping over the old contents of the file
file.close() #Closes the file (write session)
и передать:
filename=Myfile.java, sourceText='System.out.println', replaceText='if (logger.isInfoEnabled()) { \n' \logger.info'
Однако яизо всех сил, чтобы получить закрытие в замене.Он должен обернуться вокруг той же самой выходной строки, которая уже существует.Какие-нибудь советы?
Спасибо.