fix脚本添加转写为xlsx
Showing
1 changed file
with
34 additions
and
3 deletions
| ... | @@ -229,18 +229,49 @@ def main(): | ... | @@ -229,18 +229,49 @@ def main(): |
| 229 | print(f"移除行数: {len(removed)}(同歌手且时长相近)") | 229 | print(f"移除行数: {len(removed)}(同歌手且时长相近)") |
| 230 | print(f"保留行数: {len(kept)}") | 230 | print(f"保留行数: {len(kept)}") |
| 231 | 231 | ||
| 232 | # 写修复后的 CSV(去掉冗余列) | 232 | # 写修复后的文件(去掉冗余列) |
| 233 | DROP_COLS = {"topk_hit", "expected_duplicate", "predicted_duplicate", "sample_class"} | 233 | DROP_COLS = {"topk_hit", "expected_duplicate", "predicted_duplicate", "sample_class"} |
| 234 | out_fieldnames = [f for f in fieldnames if f not in DROP_COLS] | 234 | out_fieldnames = [f for f in fieldnames if f not in DROP_COLS] |
| 235 | out_path = Path(args.output) | 235 | out_path = Path(args.output) |
| 236 | with out_path.open("w", newline="", encoding="utf-8") as f: | 236 | |
| 237 | # 输出 xlsx(原生支持 Unicode,Excel 打开不乱码) | ||
| 238 | xlsx_path = out_path.with_suffix(".xlsx") | ||
| 239 | import openpyxl | ||
| 240 | from openpyxl.styles import Font, PatternFill, Alignment | ||
| 241 | wb = openpyxl.Workbook() | ||
| 242 | ws = wb.active | ||
| 243 | ws.title = "eval" | ||
| 244 | |||
| 245 | # 表头加粗 + 浅灰背景 | ||
| 246 | header_font = Font(bold=True) | ||
| 247 | header_fill = PatternFill("solid", fgColor="D9D9D9") | ||
| 248 | ws.append(out_fieldnames) | ||
| 249 | for cell in ws[1]: | ||
| 250 | cell.font = header_font | ||
| 251 | cell.fill = header_fill | ||
| 252 | cell.alignment = Alignment(horizontal="center") | ||
| 253 | |||
| 254 | # 数据行 | ||
| 255 | for r in kept: | ||
| 256 | ws.append([r.get(f, "") for f in out_fieldnames]) | ||
| 257 | |||
| 258 | # 自适应列宽(最大 40 字符) | ||
| 259 | for col in ws.columns: | ||
| 260 | max_len = max((len(str(cell.value or "")) for cell in col), default=0) | ||
| 261 | ws.column_dimensions[col[0].column_letter].width = min(max_len + 2, 40) | ||
| 262 | |||
| 263 | wb.save(xlsx_path) | ||
| 264 | print(f"已写入: {xlsx_path}") | ||
| 265 | |||
| 266 | # 同时保留 CSV(utf-8-sig,Excel 双击也能正确识别中文) | ||
| 267 | with out_path.open("w", newline="", encoding="utf-8-sig") as f: | ||
| 237 | writer = csv.DictWriter(f, fieldnames=out_fieldnames, extrasaction="ignore") | 268 | writer = csv.DictWriter(f, fieldnames=out_fieldnames, extrasaction="ignore") |
| 238 | writer.writeheader() | 269 | writer.writeheader() |
| 239 | writer.writerows(kept) | 270 | writer.writerows(kept) |
| 240 | print(f"已写入: {out_path}") | 271 | print(f"已写入: {out_path}") |
| 241 | 272 | ||
| 242 | # 重新计算 summary | 273 | # 重新计算 summary |
| 243 | summary = compute_summary(kept, threshold=args.threshold, out_path=str(out_path)) | 274 | summary = compute_summary(kept, threshold=args.threshold, out_path=str(xlsx_path)) |
| 244 | summary_path = out_path.with_suffix(".summary.json") | 275 | summary_path = out_path.with_suffix(".summary.json") |
| 245 | with summary_path.open("w", encoding="utf-8") as f: | 276 | with summary_path.open("w", encoding="utf-8") as f: |
| 246 | json.dump(summary, f, ensure_ascii=False, indent=2) | 277 | json.dump(summary, f, ensure_ascii=False, indent=2) | ... | ... |
-
Please register or sign in to post a comment