I have the following code that modifies every line of the test.tex file by replacing the regular expression.
import re import fileinput regex=re.compile(r'^([^&]*)(&)([^&]*)(&)([^&]*)') for line in fileinput.input('test.tex',inplace=1): print regex.sub(r'\3\2\1\4\5',line),
The only problem is that I want the substitution applied to certain lines in the file, and there is no way to define a pattern to select the correct lines. So, I want to display each line and ask the user on the command line, asking whether to make a replacement in the current line. If the user enters "y", a replacement is performed. If the user simply does not enter anything, the substitution is not performed.
The problem, of course, is that with the inplace=1
code, I effectively redirected stdout to an open file. Thus, there is no way to show the output (for example, the question of whether to make a replacement) on the command line that is not sent to the file.
Any ideas?
source share