简体   繁体   English

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

[英]How to skip lines when reading a yaml file in python?

I'm familiar with similar questions, but they don't seem to address what should be a simple problem. 我熟悉类似的问题,但它们似乎并没有解决应该是一个简单的问题。 I am using Python 2.7x and trying to read a YAML file that looks similar to this: 我正在使用Python 2.7x并尝试读取类似于此的YAML文件:

%YAML:1.0
radarData: !!opencv-matrix
rows: 5
cols: 2
dt: u
data: [0, 0, 0, 0, 0, 10, 5, 3, 1, 22]

For now I only need the 'data:' document. 现在我只需要'data:'文档。 I have tried a vanilla approach and then tried to force skip the first 4 lines (second code snippet that is commented out). 我尝试了一种vanilla方法然后尝试强制跳过前4行(第二个被注释掉的代码片段)。 Both approaches gave errors. 两种方法都有错误。

import yaml
stream = file('test_0x.yml', 'r') 
yaml.load(stream)
# alternative code snippet
# with open('test_0x.yml') as f:
#  stream = f.readlines()[4:]
# yaml.load(stream)

Any suggestions about how skip the first few lines would be very appreciated. 关于如何跳过前几行的任何建议都将非常感激。

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]

Skipping the first 5 lines also works. 跳过前5行也行。

I completely missed the point here, but I leave my original answer at the bottom as a humbling reminder. 我在这里完全忽略了这一点,但我将原来的答案留在底部作为一个谦卑的提醒。

mhawke's answer is short and sweet, and is probably preferable. 莫霍克的答案简短而甜蜜,可能更可取。 A more complicated solution: strip that malformed directive, correct your custom tag, and add a constructor for it. 一个更复杂的解决方案:剥离格式错误的指令,更正自定义标记,并为其添加构造函数。 This has the advantage of correcting that tag wherever it appears in a file, not just in the first couple of lines. 这样做的好处是可以在文件中出现的任何位置校正该标记,而不仅仅是在前几行中。

My implementation here does have some disadvantages - it slurps whole files, and it hasn't been tested on complex data, where the effect of replacing the tag with a proper one might have different results than intended. 我在这里的实现确实有一些缺点 - 它会玷污整个文件,并且尚未在复杂数据上进行测试,其中用正确的数据替换标记的效果可能与预期的结果不同。

import yaml

def strip_malformed_directive(yaml_file):
    """
    Strip a malformed YAML directive from the top of a file.

    Returns the slurped (!) file.
    """
    lines = list(yaml_file)
    first_line = lines[0]
    if first_line.startswith('%') and ":" in first_line:
       return "\n".join(lines[1:])
    else:
       return "\n".join(lines)


def convert_opencvmatrix_tag(yaml_events):
    """
    Convert an erroneous custom tag, !!opencv-matrix, to the correct 
    !opencv-matrix, in a stream of YAML events.
    """
    for event in yaml_events:
        if hasattr(event, "tag") and event.tag == u"tag:yaml.org,2002:opencv-matrix":
            event.tag = u"!opencv-matrix"
        yield event


yaml.add_constructor("!opencv-matrix", lambda loader, node: None)
with open("test_0x.yml") as yaml_file:
    directive_processed = strip_malformed_directive(yaml_file)
    yaml_events = yaml.parse(directive_processed)
    matrix_tag_converted = convert_opencvmatrix_tag(yaml_events)
    fixed_document = yaml.emit(matrix_tag_converted)

    data = yaml.load(fixed_document)
    print data

Original Answer 原始答案

That yaml.load function you're using returns a dictionary, which can be accessed like so: 你正在使用的yaml.load函数返回一个字典,可以这样访问:

import yaml

with open("test_0x.yml") as yaml_file:
    test_data = yaml.load(yaml_file)

print test_data["data"]

Does that help? 这有帮助吗?

I have camera matrix generated by aruco_calibration_fromimages.exe , here is yml file: 我有aruco_calibration_fromimages.exe生成的相机矩阵,这里是yml文件:

%YAML:1.0
---
image_width: 4000
image_height: 3000
camera_matrix: !!opencv-matrix
   rows: 3
   cols: 3
   dt: d
   data: [ 3.1943912478853654e+03, 0., 1.9850941722590378e+03, 0.,
       3.2021356095317910e+03, 1.5509955246019449e+03, 0., 0., 1. ]
distortion_coefficients: !!opencv-matrix
   rows: 1
   cols: 5
   dt: d
   data: [ 1.3952810090687282e-01, -3.8313647492178071e-01,
       5.0555840762660396e-03, 2.3753464602670597e-03,
       3.3952514744179502e-01 ]

Load this yml with this code: 使用以下代码加载此yml:

import cv2
fs = cv2.FileStorage("./calib_asus_chess/cam_calib_asus.yml", cv2.FILE_STORAGE_READ)
fn = fs.getNode("camera_matrix")
print(fn.mat())

And get this result: 得到这个结果:

[[  3.19439125e+03   0.00000000e+00   1.98509417e+03]
 [  0.00000000e+00   3.20213561e+03   1.55099552e+03]
 [  0.00000000e+00   0.00000000e+00   1.00000000e+00]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM