Commit 04c78716 04c78716be35293f72e98d1818b214d4f53f5cae by 沈秋雨

fix脚本添加转写为xlsx

1 parent 122cab3c
......@@ -229,18 +229,49 @@ def main():
print(f"移除行数: {len(removed)}(同歌手且时长相近)")
print(f"保留行数: {len(kept)}")
# 写修复后的 CSV(去掉冗余列)
# 写修复后的文件(去掉冗余列)
DROP_COLS = {"topk_hit", "expected_duplicate", "predicted_duplicate", "sample_class"}
out_fieldnames = [f for f in fieldnames if f not in DROP_COLS]
out_path = Path(args.output)
with out_path.open("w", newline="", encoding="utf-8") as f:
# 输出 xlsx(原生支持 Unicode,Excel 打开不乱码)
xlsx_path = out_path.with_suffix(".xlsx")
import openpyxl
from openpyxl.styles import Font, PatternFill, Alignment
wb = openpyxl.Workbook()
ws = wb.active
ws.title = "eval"
# 表头加粗 + 浅灰背景
header_font = Font(bold=True)
header_fill = PatternFill("solid", fgColor="D9D9D9")
ws.append(out_fieldnames)
for cell in ws[1]:
cell.font = header_font
cell.fill = header_fill
cell.alignment = Alignment(horizontal="center")
# 数据行
for r in kept:
ws.append([r.get(f, "") for f in out_fieldnames])
# 自适应列宽(最大 40 字符)
for col in ws.columns:
max_len = max((len(str(cell.value or "")) for cell in col), default=0)
ws.column_dimensions[col[0].column_letter].width = min(max_len + 2, 40)
wb.save(xlsx_path)
print(f"已写入: {xlsx_path}")
# 同时保留 CSV(utf-8-sig,Excel 双击也能正确识别中文)
with out_path.open("w", newline="", encoding="utf-8-sig") as f:
writer = csv.DictWriter(f, fieldnames=out_fieldnames, extrasaction="ignore")
writer.writeheader()
writer.writerows(kept)
print(f"已写入: {out_path}")
# 重新计算 summary
summary = compute_summary(kept, threshold=args.threshold, out_path=str(out_path))
summary = compute_summary(kept, threshold=args.threshold, out_path=str(xlsx_path))
summary_path = out_path.with_suffix(".summary.json")
with summary_path.open("w", encoding="utf-8") as f:
json.dump(summary, f, ensure_ascii=False, indent=2)
......