41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
"""上传文件解析。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import csv
|
|
import io
|
|
import json
|
|
from typing import BinaryIO
|
|
|
|
from openpyxl import load_workbook
|
|
|
|
|
|
def parse_upload_file(file_obj: BinaryIO, filename: str) -> list[dict]:
|
|
suffix = filename.rsplit(".", 1)[-1].lower() if "." in filename else ""
|
|
content = file_obj.read()
|
|
if suffix == "json":
|
|
data = json.loads(content.decode("utf-8-sig"))
|
|
if isinstance(data, dict):
|
|
return data.get("domains") or data.get("rows") or []
|
|
return data
|
|
if suffix == "csv":
|
|
text = content.decode("utf-8-sig")
|
|
return list(csv.DictReader(io.StringIO(text)))
|
|
if suffix in {"xlsx", "xls"}:
|
|
workbook = load_workbook(io.BytesIO(content), read_only=True, data_only=True)
|
|
try:
|
|
sheet = workbook.active
|
|
rows = list(sheet.iter_rows(values_only=True))
|
|
if not rows:
|
|
return []
|
|
headers = [str(value or "").strip() for value in rows[0]]
|
|
parsed = []
|
|
for row in rows[1:]:
|
|
item = {headers[index]: value for index, value in enumerate(row) if index < len(headers)}
|
|
if any(value not in (None, "") for value in item.values()):
|
|
parsed.append(item)
|
|
return parsed
|
|
finally:
|
|
workbook.close()
|
|
raise ValueError(f"不支持的文件类型: {filename}")
|