import re with open("/tmp/nezha-config-raw.yaml") as f: content = f.read() content = re.sub(r"custom_code:.*\n?", "", content) css = ( '' ) content = content.rstrip() + '\ncustom_code: "' + css + '"\n' with open("/tmp/nezha-config-fixed.yaml", "w") as f: f.write(content) print("done")