我熟悉类似的问题,但它们似乎并没有解决应该是一个简单的问题。我正在使用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]
现在我只需要'data:'文件。我尝试了一种vanilla方法然后尝试强制跳过前4行(第二个被注释掉的代码片段)。两种方法都有错误。
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)
有关如何跳过前几行的任何建议都将非常感激。
答案 0 :(得分:9)
实际上,你只需要跳过前两行。
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行也有效。
答案 1 :(得分:1)
我完全忽略了这一点,但我将原来的答案留在底部作为一个谦卑的提醒。
mhawke的答案简短而甜蜜,可能更可取。一个更复杂的解决方案:剥离格式错误的指令,更正自定义标记,并为其添加构造函数。这样做的好处是可以在文件中出现的任何位置校正该标记,而不仅仅是在前几行中。我在这里的实现确实有一些缺点 - 它会玷污整个文件,并且它还没有在复杂数据上进行测试,其中用正确的数据替换标记的效果可能与预期的结果不同。
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
原始答案
您正在使用的yaml.load
函数会返回一个字典,可以这样访问:
import yaml
with open("test_0x.yml") as yaml_file:
test_data = yaml.load(yaml_file)
print test_data["data"]
这有帮助吗?
答案 2 :(得分:1)
我有 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 ]
使用以下代码加载此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())
得到这个结果:
[[ 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]]