fix: 支持CSV自动识别UTF-8和GBK编码
This commit is contained in:
@@ -11,7 +11,7 @@ from starlette.background import BackgroundTask
|
||||
|
||||
from app import state
|
||||
from app.config import CACHE_DIR
|
||||
from app.database import DatabaseManager
|
||||
from app.database import DatabaseManager, detect_csv_encoding
|
||||
from app.services.platform import make_client
|
||||
from app.utils.files import remove_file_safely
|
||||
from app.warehouse import make_cell_data_warehouse, make_warehouse
|
||||
@@ -255,8 +255,8 @@ def download_table_template(
|
||||
)
|
||||
|
||||
|
||||
def _read_csv_header(path: Path) -> list[str]:
|
||||
with path.open("r", encoding="utf-8-sig", newline="") as handle:
|
||||
def _read_csv_header(path: Path, encoding: str) -> list[str]:
|
||||
with path.open("r", encoding=encoding, newline="") as handle:
|
||||
for row in csv.reader(handle):
|
||||
return [str(cell).strip() for cell in row]
|
||||
return []
|
||||
@@ -284,7 +284,8 @@ def import_table_csv(
|
||||
tmp_path = CACHE_DIR / f"import_{timestamp}.csv"
|
||||
tmp_path.write_bytes(file.file.read())
|
||||
try:
|
||||
header = _read_csv_header(tmp_path)
|
||||
encoding = detect_csv_encoding(str(tmp_path))
|
||||
header = _read_csv_header(tmp_path, encoding)
|
||||
if not header:
|
||||
raise HTTPException(status_code=400, detail="CSV 文件为空或缺少表头")
|
||||
missing = [name for name in columns if name not in header]
|
||||
@@ -296,7 +297,7 @@ def import_table_csv(
|
||||
if extra:
|
||||
parts.append("多余字段: " + ", ".join(extra))
|
||||
raise HTTPException(status_code=400, detail="CSV 字段与模板不一致,导入失败。" + ";".join(parts))
|
||||
imported = db.import_csv(str(tmp_path), table_name)
|
||||
imported = db.import_csv(str(tmp_path), table_name, encoding=encoding)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user