fix_encoding.py 886 B

123456789101112131415161718192021222324252627
  1. """修复 openai_compat_service.py 中被 PowerShell Set-Content 损坏的编码"""
  2. import re
  3. path = 'backend/app/services/openai_compat_service.py'
  4. with open(path, 'rb') as f:
  5. raw = f.read()
  6. # PowerShell Set-Content 用 UTF-16LE 写入,但文件头没有 BOM,导致中文乱码
  7. # 尝试以 utf-16-le 解码
  8. try:
  9. text = raw.decode('utf-16-le')
  10. print("Decoded as utf-16-le")
  11. except Exception:
  12. # 已经是 utf-8,只是有替换字符 \ufffd
  13. text = raw.decode('utf-8', errors='replace')
  14. print("Decoded as utf-8 with replacement")
  15. # 修复已知的损坏模式:注释末尾被截断后紧跟下一行代码
  16. # 模式:中文注释 + \ufffd + 空格 + 代码
  17. text = re.sub(r'[\ufffd\x00]+\s*', '\n ', text)
  18. # 写回 utf-8
  19. with open(path, 'w', encoding='utf-8') as f:
  20. f.write(text)
  21. print("Done. Check the file manually for remaining issues.")