import sys with open('core/templates/core/yard_sign_voters.html', 'r') as f: lines = f.readlines() new_lines = [] skip_next_th = False skip_next_td = False # Remove duplicated TH for i, line in enumerate(lines): if 'Voters Wanting SignSign Type' in line: new_lines.append(line) skip_next_th = True continue if skip_next_th and 'Sign Type' in line: skip_next_th = False continue # Remove duplicated TD if '' in line and i + 3 < len(lines) and 'household.sign_types_display' in lines[i+1] and 'bg-warning' in lines[i+1] and '' in lines[i+2] and '' in lines[i+3]: if skip_next_td: # This is the second one, skip it skip_next_td = False # We need to skip 4 lines # I'll just not append them and increment a counter or something # Actually, let's just use a more robust approach. pass else: # This is the first one, keep it but mark that we should skip the next one new_lines.append(line) skip_next_td = True continue # This logic is a bit flawed for multi-line. Let's try something else. new_lines.append(line) # Let's try a simpler approach: replace the exact block if it's there. content = "".join(lines) # Fix duplicated TH old_th = 'Voters Wanting SignSign Type\n Sign Type' new_th = 'Voters Wanting Sign\n Sign Type' content = content.replace(old_th, new_th) # Fix duplicated TD # Note: I already fixed the { % if so both are same now old_td = """ {{ household.sign_types_display }} {{ household.sign_types_display }} """ new_td = """ {{ household.sign_types_display }} """ content = content.replace(old_td, new_td) with open('core/templates/core/yard_sign_voters.html', 'w') as f: f.write(content)