次のようなファイルからxyzのデータだけ抽出したい
header:
seq: 1
stamp:
secs: 1666429992
nsecs: 873829108
frame_id: "camera_depth_optical_frame"
point:
x: -0.027914434671401978
y: 0.02820553630590439
z: 0.5456792712211609
---
header:
seq: 2
stamp:
secs: 1666430004
nsecs: 579051551
frame_id: "camera_depth_optical_frame"
point:
x: -0.028040047734975815
y: -0.01734834909439087
z: 0.5262469053268433
---
header:
seq: 3
stamp:
secs: 1666430018
nsecs: 916446553
frame_id: "camera_depth_optical_frame"
point:
x: 0.07294759899377823
y: -0.0181780606508255
z: 0.5236693620681763
---
header:
seq: 4
stamp:
secs: 1666430027
nsecs: 753083919
frame_id: "camera_depth_optical_frame"
point:
x: 0.07170296460390091
y: 0.03082507848739624
z: 0.5377740859985352
---
header:
seq: 5
stamp:
secs: 1666430080
nsecs: 448720876
frame_id: "camera_depth_optical_frame"
point:
x: -0.029203645884990692
y: 0.03928879648447037
z: 0.5081203579902649
---
header:
seq: 6
stamp:
secs: 1666430093
nsecs: 199850911
frame_id: "camera_depth_optical_frame"
point:
x: -0.02828790619969368
y: -0.006579136475920677
z: 0.48882704973220825
---
header:
seq: 7
stamp:
secs: 1666430105
nsecs: 607030476
frame_id: "camera_depth_optical_frame"
point:
x: 0.07050076127052307
y: -0.005021311342716217
z: 0.4861147403717041
---
header:
seq: 8
stamp:
secs: 1666430115
nsecs: 348506727
frame_id: "camera_depth_optical_frame"
point:
x: 0.06972650438547134
y: 0.040438100695610046
z: 0.5042555332183838
---
次のようなプログラムを作成
get_xyz.py
p = [0,0,0]
with open('./data1022') as f:
for line in f:
if "x" in line:
p[0] = (float(line.split()[1]))
if "y" in line:
p[1] = (float(line.split()[1]))
if "z" in line:
p[2] = (float(line.split()[1]))
print(p)
結果
[-0.027914434671401978, 0.02820553630590439, 0.5456792712211609]
[-0.028040047734975815, -0.01734834909439087, 0.5262469053268433]
[0.07294759899377823, -0.0181780606508255, 0.5236693620681763]
[0.07170296460390091, 0.03082507848739624, 0.5377740859985352]
[-0.029203645884990692, 0.03928879648447037, 0.5081203579902649]
[-0.02828790619969368, -0.006579136475920677, 0.48882704973220825]
[0.07050076127052307, -0.005021311342716217, 0.4861147403717041]
[0.06972650438547134, 0.040438100695610046, 0.5042555332183838]