diff --git a/app.py b/app.py index 9a7f55d..c689b72 100644 --- a/app.py +++ b/app.py @@ -22,7 +22,6 @@ import shutil from flask_limiter import Limiter import flask -import nkf import tjaf # ---- @@ -812,8 +811,25 @@ def upload_file(): return flask.jsonify({'error': 'ファイルが選択されていません'}) # TJAファイルをテキストUTF-8/LFに変換 - tja_data = nkf.nkf('-wd', file_tja.read()) - tja_text = tja_data.decode("utf-8") + tja_data = file_tja.read() + # 尝试检测编码并转换为UTF-8 + try: + # 首先尝试UTF-8解码 + tja_text = tja_data.decode("utf-8") + except UnicodeDecodeError: + # 如果UTF-8失败,尝试其他常见编码 + for encoding in ['shift_jis', 'euc-jp', 'iso-2022-jp', 'cp932']: + try: + tja_text = tja_data.decode(encoding) + break + except UnicodeDecodeError: + continue + else: + # 如果所有编码都失败,使用错误处理 + tja_text = tja_data.decode("utf-8", errors="replace") + + # 删除回车符(CR),只保留换行符(LF) + tja_text = tja_text.replace('\r', '') print("TJAのサイズ:",len(tja_text)) # TJAファイルの内容を解析 tja = tjaf.Tja(tja_text)