使用python解析带有逗号分隔的多个键的yaml

2024-09-26 17:43:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图阅读一个由商业软件生成的YAML文件。test.yml的摘录如下:

%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]

然后,在使用python解析该文件时:

^{pr2}$

关于如何解析数据有什么建议吗?我刚开始学习YAML格式,和python都有,所以有点困惑。在


Tags: 文件nametestyamlymllengthmass商业软件
1条回答
网友
1楼 · 发布于 2024-09-26 17:43:25

使用列表解包在不使用循环的情况下提取列表。在

x_moment, y_moment, z_moment = data['VesselTypes'][0]['Draughts'][0]['MomentOfInertiaTensorX, MomentOfInertiaTensorY, MomentOfInertiaTensorZ']

相关问题 更多 >

    热门问题