Update app.py

This commit is contained in:
AnthonyDuan1128
2025-10-06 20:54:54 +08:00
committed by GitHub
parent 6a9bc44ed8
commit 1e6a7063a2

22
app.py
View File

@@ -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)