市販のソフトウェアで生成されたYAMLファイルを読み込もうとしています。 test.yml
の抜粋は次のようになります。Pythonを使用して複数のキーをカンマで区切ってyamlを解析する
%YAML 1.1
---
VesselTypes:
- Name: Tanker
Length: 103
Draughts:
- Name: Draught1
Mass: 9017.95
MomentOfInertiaTensorX, MomentOfInertiaTensorY, MomentOfInertiaTensorZ:
- [254.9374465E3, 1, 2]
- [3, 5.979802645E6, 4]
- [7, 8, 5.979802645E6]
- [9, 10, 11]
その後、パイソン使ってこのファイルを解析中:データが解析されることが予想されている方法についての
#filename: test.py
import yaml
with open('test.yml', 'r') as f:
data = yaml.load(f)
print(data['VesselTypes'][0]['Draughts'][0]['Name'])
#This would work
print(data['VesselTypes'][0]['Draughts'][0]['MomentOfInertiaTensorX, MomentOfInertiaTensorY, MomentOfInertiaTensorZ'])
print('----------------------------------------------------')
#This would give error
print(data['VesselTypes'][0]['Draughts'][0]['MomentOfInertiaTensorX'])
任意の提案を?私はちょうどYAML形式とPythonの両方を学び始めました。少し混乱しました。
' 'MomentOfInertiaTensorX、MomentOfInertiaTensorY、' [[254.9374465E3、0、0]、...] 'マトリックスへの鍵をMomentOfInertiaTensorZ''is。したがって、エラーはあなたが期待するものです! –
私は彼らが単なる情報ヘッダーよりも役に立つと思っていました。したがって、列として値を格納するには、今度は反復ループを実行する必要があります。 – Amitava
私は、コンマで区切られたキーの "アンパック"は実際にYAMLがどのように動作するのかとは思いません。 'MomentOfInertiaTensorX:[254.9374465E3,0,0]'のような3行が必要になるでしょう。 –