fix: 支持CSV自动识别UTF-8和GBK编码
This commit is contained in:
+16
-2
@@ -9,6 +9,19 @@ from typing import Any, Dict, List, Optional, Tuple
|
||||
from app.config import AppConfig, MySQLConfig
|
||||
|
||||
|
||||
def detect_csv_encoding(file_path: str) -> str:
|
||||
"""识别数据管理导入 CSV 的 UTF-8 或 GBK 系列编码。"""
|
||||
for encoding in ("utf-8-sig", "gb18030"):
|
||||
try:
|
||||
with open(file_path, "r", encoding=encoding, errors="strict") as handle:
|
||||
while handle.read(1024 * 1024):
|
||||
pass
|
||||
return encoding
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
raise UnicodeError("CSV 编码无法识别,仅支持 UTF-8 或 GBK 编码")
|
||||
|
||||
|
||||
class DatabaseManager:
|
||||
"""数据库管理器"""
|
||||
|
||||
@@ -236,9 +249,10 @@ class DatabaseManager:
|
||||
conn.commit()
|
||||
return cursor.rowcount
|
||||
|
||||
def import_csv(self, file_path: str, table_name: str) -> int:
|
||||
def import_csv(self, file_path: str, table_name: str, encoding: str | None = None) -> int:
|
||||
"""按 CSV 表头列追加导入(列须与表字段一致,由调用方校验)。返回导入行数。"""
|
||||
with open(file_path, "r", encoding="utf-8-sig", newline="") as handle:
|
||||
csv_encoding = encoding or detect_csv_encoding(file_path)
|
||||
with open(file_path, "r", encoding=csv_encoding, newline="") as handle:
|
||||
reader = csv.reader(handle)
|
||||
try:
|
||||
header = next(reader)
|
||||
|
||||
Reference in New Issue
Block a user