Jan-21-2018, 08:52 PM
(This post was last modified: Jan-21-2018, 08:53 PM by Gribouillis.)
If you think it's easier, you can add
{}
with the re module:for line in file: line = re.sub(r'^(\s*)(begin\s)', r'\1{\2', line) line = re.sub(r'^(\s*)end(\s)', r'\1end}\2', line) print(line)