Aug-07-2022, 07:27 AM
(This post was last modified: Aug-07-2022, 07:29 AM by Gribouillis.)
The slow part is the accumulative string concatenation at line 8 (the + operator). Try this code
def good_lines(file): for line in file: line = line.strip() if line[:1] == '[' and line[-1] == ']': yield line + '\n' with open('input.raw') as ifh, open('output.raw', 'w') as ofh: ofh.writelines(good_lines(ifh))