fix: 仅对储存历史CSV使用GBK
This commit is contained in:
@@ -82,7 +82,8 @@ LTE_PLMN = "460-00"
|
||||
DATABASE_NAME = "interference_etl"
|
||||
DATABASE_TABLE = "interference_hourly_summary"
|
||||
DEFAULT_HISTORY_ROOT = f"{DEFAULT_ROOT}/干扰历史数据"
|
||||
CSV_ENCODING = "gbk"
|
||||
LOCAL_CSV_ENCODING = "utf-8-sig"
|
||||
HISTORY_CSV_ENCODING = "gbk"
|
||||
|
||||
HEADER_FDD = (
|
||||
"开始时间",
|
||||
@@ -806,7 +807,10 @@ def process(
|
||||
stored_rows = store.rows_for_time(metric_time)
|
||||
if not stored_rows:
|
||||
raise ProcessingError(f"Database contains no rows for {metric_time_text}")
|
||||
history_path = history.upload(metric_time, dict_csv_bytes(SUMMARY_HEADER, stored_rows))
|
||||
history_path = history.upload(
|
||||
metric_time,
|
||||
dict_csv_bytes(SUMMARY_HEADER, stored_rows, HISTORY_CSV_ENCODING),
|
||||
)
|
||||
print("status=history_repaired")
|
||||
print(f"target_time={metric_time_text}")
|
||||
print(f"history_output={history_path}")
|
||||
@@ -867,7 +871,10 @@ def process(
|
||||
if store is not None:
|
||||
database_result = store.replace_latest(summary_rows)
|
||||
if history is not None:
|
||||
history_path = history.upload(metric_time, dict_csv_bytes(SUMMARY_HEADER, summary_rows))
|
||||
history_path = history.upload(
|
||||
metric_time,
|
||||
dict_csv_bytes(SUMMARY_HEADER, summary_rows, HISTORY_CSV_ENCODING),
|
||||
)
|
||||
history_result = {"enabled": True, "path": history_path}
|
||||
matched_coordinates = sum(bool(row["longitude"] and row["latitude"]) for row in summary_rows)
|
||||
manifest = {
|
||||
@@ -1087,24 +1094,31 @@ def sql_decimal_literal(value: str, field: str, nullable: bool = False) -> str:
|
||||
|
||||
|
||||
def write_csv(path: Path, header: tuple[str, ...], rows: list[tuple[object, ...]]) -> None:
|
||||
with path.open("w", encoding=CSV_ENCODING, newline="") as file:
|
||||
with path.open("w", encoding=LOCAL_CSV_ENCODING, newline="") as file:
|
||||
writer = csv.writer(file)
|
||||
writer.writerow(header)
|
||||
for row in rows:
|
||||
writer.writerow(normalize_csv_cell(value) for value in row)
|
||||
writer.writerow(normalize_cell(value) for value in row)
|
||||
|
||||
|
||||
def write_dict_csv(path: Path, header: tuple[str, ...], rows: list[dict[str, str]]) -> None:
|
||||
path.write_bytes(dict_csv_bytes(header, rows))
|
||||
|
||||
|
||||
def dict_csv_bytes(header: tuple[str, ...], rows: list[dict[str, str]]) -> bytes:
|
||||
def dict_csv_bytes(
|
||||
header: tuple[str, ...],
|
||||
rows: list[dict[str, str]],
|
||||
encoding: str = LOCAL_CSV_ENCODING,
|
||||
) -> bytes:
|
||||
output = io.StringIO(newline="")
|
||||
writer = csv.DictWriter(output, fieldnames=header, extrasaction="raise")
|
||||
writer.writeheader()
|
||||
for row in rows:
|
||||
writer.writerow({column: normalize_csv_cell(row[column]) for column in header})
|
||||
return output.getvalue().encode(CSV_ENCODING)
|
||||
if encoding == HISTORY_CSV_ENCODING:
|
||||
writer.writerow({column: normalize_csv_cell(row[column]) for column in header})
|
||||
else:
|
||||
writer.writerow(row)
|
||||
return output.getvalue().encode(encoding)
|
||||
|
||||
|
||||
def ensure_scoped(root: Path, target: Path) -> None:
|
||||
|
||||
Reference in New Issue
Block a user