with open("scratch/restore_backtest_safe.py", "r", encoding="utf-8") as f: text = f.read() # Replace surrogate pairs with actual characters or proper escapes # \ud83c\udfc6 -> πŸ† text = text.replace(r"\ud83c\udfc6", "πŸ†") # We also have \ube44 (λΉ„), \uad50 (ꡐ) etc, but those are just normal BMP characters, # but they might be encoded as surrogates? No, BMP is fine. with open("scratch/restore_backtest_safe.py", "w", encoding="utf-8") as f: f.write(text) print("Fixed surrogates")