重构手部分段查表逻辑并新增 Excel 数据提取脚本

- src/app.rs: 将 update_hand_signal_histories 改为返回总力作为汇总路径;新增 raw_to_g6/raw_to_g7 与 raw_to_hand_segment_g 索引映射,把 segment 5/6 从兜底 raw_to_gd 拆出,并按 H 版本校准数据更新 g1~g5 的查表数组
This commit is contained in:
lenn
2026-07-08 17:45:27 +08:00
parent 9fb4c0edf4
commit 5af3c2862b
2 changed files with 377 additions and 26 deletions

View File

@@ -0,0 +1,275 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
提取 展会数据-H版本.xlsx 中每一组数据并按指定格式输出。
数据组织5 个数据组1#、3#、7#、8#、10#),由空行分隔。
每组包含 10 行指标:
指标 / 1s 均值 / 240s 输出值 / 240s 均值 / 误差(%) /
精度 / b值平均值 / AD值增幅 / AD值漂移 / 增幅/漂移
每行有 12 个数值列C~N与 指标 行的 12 个挡位一一对应。
输出格式(示例):
1#, 115181, 365308, 485911, ...,
3#, 116825, 371568, 475160, ...,
用法:
python extract_excel_data.py [xlsx_path] [选项]
--row LABEL 只输出指定指标行(如 "1s 均值"),可多次指定
--skip-indicators V 跳过指定挡位(按 指标 行数值匹配),可多次指定
--with-title 在每行首部加上组编号(如 "1#, 115181, ..."
--raw 保留原始数值(不取整)
--annotate 在每行末尾附加 " // 组号 指标名" 注释
-o FILE 输出到文件(默认输出到标准输出)
"""
import argparse
import os
import sys
import openpyxl
# 默认 Excel 文件路径
DEFAULT_XLSX = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"..",
"展会数据-H版本.xlsx",
)
def is_blank_row(row_values):
"""判断是否为空白行(全为 None 或空字符串)。"""
for v in row_values:
if v is None:
continue
if isinstance(v, str) and v.strip() == "":
continue
return False
return True
def is_group_header(row_values):
"""判断是否为组标题行A 列为类似 '1#' '3#' 的编号)。"""
first = row_values[0]
if first is None:
return False
return isinstance(first, str) and first.strip().endswith("#")
def extract_data_rows(ws):
"""
返回一个列表,每个元素是 (group_name, row_label, [12 个数值])。
group_name 为当前组编号row_label 为 B 列标签;数值列表为 C~N 列。
"""
results = []
current_group = None
for row in ws.iter_rows(min_row=1, max_row=ws.max_row, values_only=True):
row = list(row[:14]) # 仅取前 14 列
if is_blank_row(row):
continue
if is_group_header(row):
current_group = row[0]
label = row[1] if len(row) > 1 and row[1] is not None else "指标"
values = row[2:]
results.append((current_group, label, values))
continue
label = row[1] if len(row) > 1 and row[1] is not None else ""
values = row[2:]
if current_group is None:
continue
results.append((current_group, label, values))
return results
def parse_skip_indicators(values):
"""
把命令行传入的 --skip-indicators 值解析为浮点数集合。
支持 "0.97""0.97,1.97" 形式。
"""
out = set()
if not values:
return out
for raw in values:
for token in str(raw).split(","):
token = token.strip()
if not token:
continue
try:
out.add(float(token))
except ValueError:
print(f"警告:无法解析指标值 '{token}',已忽略。", file=sys.stderr)
return out
def indicators_match(a, b, tol=1e-9):
"""浮点数比较(容忍微小误差)。"""
if a is None or b is None:
return False
try:
return abs(float(a) - float(b)) < tol
except (TypeError, ValueError):
return False
def format_value(v, raw=False):
"""
将单元格值格式化为字符串。
raw=False: 数值四舍五入到整数(匹配示例样式)。
raw=True : 保留原始数值。
"""
if v is None:
return "0"
if isinstance(v, bool):
return "1" if v else "0"
if isinstance(v, (int,)):
return str(v)
if isinstance(v, float):
if raw:
if v.is_integer():
return str(int(v))
return f"{v:g}" if abs(v) >= 1e-4 else repr(v)
return str(int(round(v)))
s = str(v).strip()
if s == "":
return "0"
try:
f = float(s)
if raw:
if f.is_integer():
return str(int(f))
return f"{f:g}"
return str(int(round(f)))
except ValueError:
return s
def format_line(values, raw=False):
"""把数值列表格式化为 'v1, v2, ..., vn,' 形式。"""
return ", ".join(format_value(v, raw=raw) for v in values) + ","
def parse_args():
p = argparse.ArgumentParser(
description="提取展会数据 Excel 中的每一行数据。",
)
p.add_argument(
"xlsx",
nargs="?",
default=DEFAULT_XLSX,
help="Excel 文件路径(默认: 展会数据-H版本.xlsx",
)
p.add_argument(
"--row",
action="append",
default=None,
metavar="LABEL",
help='只输出指定指标行(如 "1s 均值"),可多次指定。'
"未指定时输出全部行。",
)
p.add_argument(
"--skip-indicators",
action="append",
default=None,
metavar="V",
help="跳过指定挡位(按 指标 行数值匹配)。可多次指定,"
"也支持逗号分隔。例: --skip-indicators 0.97 --skip-indicators 1.97",
)
p.add_argument(
"--with-title",
action="store_true",
help="在每行首部加上组编号(如 \"1#, 115181, 365308, ...\"",
)
p.add_argument(
"--raw",
action="store_true",
help="保留原始数值(不取整)",
)
p.add_argument(
"--annotate",
action="store_true",
help="在每行末尾附加 // 组号 指标名 注释",
)
p.add_argument(
"-o",
"--output",
default=None,
help="输出文件路径(默认输出到标准输出)",
)
return p.parse_args()
def main():
args = parse_args()
xlsx_path = os.path.abspath(args.xlsx)
if not os.path.exists(xlsx_path):
print(f"错误:找不到文件 {xlsx_path}", file=sys.stderr)
return 1
wb = openpyxl.load_workbook(xlsx_path, data_only=True)
ws = wb.active
rows = extract_data_rows(ws)
if not rows:
print("未找到任何数据。", file=sys.stderr)
return 1
# ===== 列筛选(先做,依赖 指标 行) =====
skip_set = parse_skip_indicators(args.skip_indicators)
if skip_set:
filtered = []
current_indicators = None
for group, label, values in rows:
if label == "指标":
current_indicators = values
if current_indicators is None:
# 没有任何组提供 指标 行作对照,原样保留
filtered.append((group, label, values))
continue
kept = [
v for v, ind in zip(values, current_indicators)
if not any(indicators_match(ind, s) for s in skip_set)
]
filtered.append((group, label, kept))
rows = filtered
# ===== 行筛选(在列筛选之后) =====
if args.row:
wanted = {name.strip() for name in args.row}
rows = [(g, l, v) for (g, l, v) in rows if l in wanted]
if not rows:
print(
f"未找到匹配的行 {args.row}。可用行名请参考 Excel 文件 B 列。",
file=sys.stderr,
)
return 1
out_lines = []
last_group = None
for group, label, values in rows:
if last_group is not None and group != last_group:
out_lines.append("") # 组之间插入空行
last_group = group
line = format_line(values, raw=args.raw)
if args.with_title:
line = f"{group}, {line}"
if args.annotate:
line = f"{line} // {group} {label}"
out_lines.append(line)
text = "\n".join(out_lines) + "\n"
if args.output:
with open(args.output, "w", encoding="utf-8") as f:
f.write(text)
print(f"已写入 {len(rows)} 行数据到 {args.output}", file=sys.stderr)
else:
sys.stdout.write(text)
return 0
if __name__ == "__main__":
sys.exit(main())