You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python处理机器学习TXT手部数据实现结构化输出方案咨询

Python手部姿态TXT数据处理工具

支持功能

  • 输出结构化CSV表:所有字段单独成列,数值自动对应填入
  • 输出纯数值文本:仅保留所有有效数值,按帧顺序排列
  • 仅依赖Python标准库,无需额外安装第三方包

可直接运行代码

import re
import csv

# -------------------------- 配置参数 --------------------------
input_file_path = "你的输入文件名.txt"  # 替换为你的TXT文件路径
output_mode = 1  # 1=输出结构化CSV,2=输出纯数值TXT
structured_output_path = "结构化结果.csv"
numeric_output_path = "纯数值结果.txt"
# -------------------------------------------------------------

# 预定义正则匹配规则
pattern_dict = {
    "frame_id": re.compile(r'Frame.id:\s*(\d+)'),
    "timestamp": re.compile(r'Timestamp:\s*(\d+)'),
    "hand_num": re.compile(r'Hand_number:\s*(\d+)'),
    "hand_id_num": re.compile(r'hand_Id_type:\s*(\d+)'),
    "finger_num": re.compile(r"hand_finger's_number:\s*(\d+)"),
    "hand_dir": re.compile(r'hand direction:\s*\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "palm_pos": re.compile(r'Palm position:\s*\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "palm_normal": re.compile(r'Palm normal:\s*\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "thumb": re.compile(r'TYPE_THUMB\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "index": re.compile(r'TYPE_INDEX\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "middle": re.compile(r'TYPE_MIDDLE\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "ring": re.compile(r'TYPE_RING\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)'),
    "pinky": re.compile(r'TYPE_PINKY\(([-\d.]+),\s*([-\d.]+),\s*([-\d.]+)\)')
}

# 结构化表头
csv_headers = [
    'Frame.id', 'Timestamp', 'Hand_number', 'hand_Id_type数字', 'hand_finger数量',
    '手方向x', '手方向y', '手方向z',
    '手掌位置x', '手掌位置y', '手掌位置z',
    '手掌法向量x', '手掌法向量y', '手掌法向量z',
    '拇指指尖x', '拇指指尖y', '拇指指尖z',
    '食指指尖x', '食指指尖y', '食指指尖z',
    '中指指尖x', '中指指尖y', '中指指尖z',
    '无名指指尖x', '无名指指尖y', '无名指指尖z',
    '小指指尖x', '小指指尖y', '小指指尖z'
]

def parse_frame(frame_content):
    """解析单帧数据,返回数值列表"""
    res = []
    # 提取单值字段
    res.append(pattern_dict["frame_id"].search(frame_content).group(1))
    res.append(pattern_dict["timestamp"].search(frame_content).group(1))
    res.append(pattern_dict["hand_num"].search(frame_content).group(1))
    res.append(pattern_dict["hand_id_num"].search(frame_content).group(1))
    res.append(pattern_dict["finger_num"].search(frame_content).group(1))
    # 提取三元组坐标字段
    for key in ["hand_dir", "palm_pos", "palm_normal", "thumb", "index", "middle", "ring", "pinky"]:
        match = pattern_dict[key].search(frame_content)
        res.extend([match.group(1), match.group(2), match.group(3)])
    return res

if __name__ == "__main__":
    # 读取文件按帧分组
    all_frames = []
    current_frame = []
    with open(input_file_path, 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
            if line.startswith("Frame.id"):
                if current_frame:
                    all_frames.append(" ".join(current_frame))
                current_frame = [line]
            else:
                current_frame.append(line)
        # 加入最后一帧
        if current_frame:
            all_frames.append(" ".join(current_frame))
    
    # 解析所有帧
    parsed_data = [parse_frame(frame) for frame in all_frames]
    
    # 按模式输出
    if output_mode == 1:
        with open(structured_output_path, 'w', encoding='utf-8-sig', newline='') as f:
            writer = csv.writer(f)
            writer.writerow(csv_headers)
            writer.writerows(parsed_data)
        print(f"结构化CSV已生成:{structured_output_path}")
    else:
        with open(numeric_output_path, 'w', encoding='utf-8') as f:
            for row in parsed_data:
                f.write(" ".join(row) + "\n")
        print(f"纯数值文本已生成:{numeric_output_path}")

使用方法

  1. 将代码与你的TXT数据文件放在同一文件夹
  2. 修改代码中input_file_path参数为你的TXT文件名
  3. 按需修改output_mode参数:选1输出结构化CSV,选2输出纯数值TXT
  4. 直接运行代码即可在同目录得到结果文件

内容的提问来源于stack exchange,提问作者Saad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 09:36:06