Feb-18-2023, 10:35 AM
Hello,
The goal is to read text file, modify the content, then write this modified content to file:
Here is code that doesn't work:
Thanks.
The goal is to read text file, modify the content, then write this modified content to file:
Here is code that doesn't work:
import os import re file = 'aaa.txt' with open(file, 'r') as f: lines = f.readlines() for line in lines: if re.match('\d\d\:\d\d', line): ind = lines.index(line) removed_lines_index = [i for i in range(ind)] for ind in sorted(removed_lines_index, reverse = True): del lines[ind] for line in lines: f.write(line)The content of aaa.txt is something like this:
Output:hfkjsahlhlksjakls
zziiiiiiiiiii
gggggggggggg
hhhhhhhhhhhhh
klfjlksjlfkvmfklm
00:00
kjsbnvjkfdkhfjkbksdf
jdjajpofjojgaps
jlkjlkajlvjlal
Any suggestions ?Thanks.