且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在python中读取yaml文件时如何跳过行?

更新时间:2021-11-11 00:52:01

其实你只需要跳过前两行.

Actually, you only need to skip the first 2 lines.

import yaml

skip_lines = 2
with open('test_0x.yml') as infile:
    for i in range(skip_lines):
        _ = infile.readline()
    data = yaml.load(infile)

>>> data
{'dt': 'u', 'rows': 5, 'data': [0, 0, 0, 0, 0, 10, 5, 3, 1, 22], 'cols': 2}
>>> data['data']
[0, 0, 0, 0, 0, 10, 5, 3, 1, 22]

跳过前 5 行也有效.

Skipping the first 5 lines also works.