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