首页 > 解决方案 > .txt 文件在 python 中处理 ML 数据

问题描述

我有包含 ML 数据的 .txt 文件。每个文件有 19 个条目,这些条目在文本文件中一次又一次地重复。我需要在 python 中以这样的方式获取这些文件,即每个条目都成为一列,并且它们相应的数值被放置在该列中。我需要对 txt 文件中设置的所有 19 个条目执行此操作(如下面的“1”)。或仅包含数字数据的文本文件也可以(如下面的“2”)。如果您可以在python中为此编写一个很棒的代码,否则请描述一些详细信息如何做到这一点

我想要的输出:

1点击查看图片 或 2点击查看图片

文本文件:

Frame.id: 263126,Timestamp: 697287019071, Hand_number: 1,hand_Id_type: 238右手,hand_finger's_number:2,手方向: (-0.142081, 0.865413, -0.480493),手掌位置: (35.2841, 284.522, 330),8.正常:(-0.686854,-0.435733,-0.581694),Finger_type(tipposition)type_thumb(-36.7239,301.602,330.845),Finger_Type(Tipposition)type_index(typosition) , 321.318) ,Finger_type(tipposition) TYPE_RING(20.0886, 251.219, 320.136) ,Finger_type(tipposition) TYPE_PINKY(27.5919, 259.584, 310.508) Frame.id: 263127,Timestamp: 697287037765, Hand_number: 1 ,hand_Id_type: 238right hand ,hand_finger' s_number:2,手的方向:(-0.167599,0.827034,-0.536587),手掌位置:(31.7441,283.449,322.619),手掌正常:(-0.776892,-0.445873,-0.444562),Finger_type(tipposition)type_thumb(-38.8083,304.444,330.446),Finger_type(tipposition)type_index(-22.0532,344.008,273.496),Finger_type(tipposition)type_middle(type) 250.236, 317.138) ,Finger_type(tipposition) TYPE_PINKY(20.1892, 257.352, 305.739) Frame.id: 263128,Timestamp: 697287057570, Hand_number: 1 ,hand_Id_type: 238右手,hand_finger's_number:12,hand_finger's_number:19,19.0. , -0.547284) ,手掌位置: (30.8754, 280.871, 315.444) ,手掌法线: (-0.750039, -0.473481, -0.461797) ,Finger_type(tipposition) TYPE_THUMB(-40.4781, 299.689, 321.24TYPE) -23.5209, 339.286, 264.435) ,Finger_type(tipposition) TYPE_MIDDLE(-0.157164, 254.483, 311.627) ,Finger_type(tipposition) TYPE_RING(14.1716, 247.067, 309.742) ,Finger_type(tipposition) TYPE_PINKY(20.45, 254.358, 298.274) Frame.id: 263129,Timestamp: 697287076710, Hand_number: 1,hand_Id_type: 238右手,hand_finger's_number:2,hand direction: (-0.191306, 0.830611, -0)。手掌位置:(28.6877, 277.055, 299.705) ,手掌正常位置: (-0.739979, -0.472093, -0.479124) ,Finger_type(tipposition) TYPE_THUMB(-42.9838, 294.545, 305.915) ,Finger_type(tipposition) 26_TYPE(3-296.INDEX) 250.27) ,Finger_type(tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909) ,Finger_type(tipposition) TYPE_PINKY(18.5831,.4,655)2866877,277.055,299.705),棕榈正常:(-0.739979,-0.472093,-0.479124),Finger_type(tipposition)type_thumb(-42.9838,29838,294.545,294.545,305.915) (tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909) ,Finger_type(tipposition) TYPE_PINKY(18.5831, 250.968, 281.465)6877,277.055,299.705),棕榈正常:(-0.739979,-0.472093,-0.479124),Finger_type(tipposition)type_thumb(-42.9838,29838,294.545,294.545,305.915) (tipposition) TYPE_MIDDLE(-1.90294, 250.26, 294.934) ,Finger_type(tipposition) TYPE_RING(12.5955, 243.157, 292.909) ,Finger_type(tipposition) TYPE_PINKY(18.5831, 250.968, 281.465)

文本文件链接:https ://drive.google.com/file/d/1X1NdQNYlQWuNpmzGL6Wwi_SqFjIXnRbb/view?usp=sharing

标签: pythonfiletext

解决方案


我做了一些简单的争论并将其制成 CSV。希望这对你有用:

lines = []
with open("data.txt") as f:
    lines=f.readlines()

# Columns in file    
header = "Frame.id,Timestamp,Hand_number,hand_Id_type,hand_finger's_number,hand direction,\
Palm position,Palm normal,Finger_type(tipposition) TYPE_THUMB,\
Finger_type(tipposition) TYPE_INDEX,Finger_type(tipposition) TYPE_MIDDLE,\
Finger_type(tipposition) TYPE_RING,Finger_type(tipposition) TYPE_PINKY\n"
out_lines = [header] # to store rows in output file
tmp=[] # empty list to store one row
for line in lines[1:]:
    tmp.append(line.strip()) # strip and append
    # if line has last column in row, add it to tmp, make the line
    # add the line to list of rows and clear tmp
    if line.strip().startswith(",Finger_type(tipposition) TYPE_PINKY"):
        new_line = ''.join(tmp)+'\n'
        for column in header.split(','):
            # replace header keywords and :
            new_line = new_line.replace(column.strip(), '').replace(':','')
        out_lines.append(new_line)
        tmp=[]
        
print(''.join(out_lines))

# Uncomment the below block to write the csv file     
# with open("output.csv","w+") as f:
#     f.writelines(out_lines)

推荐阅读