问题描述
我有一个命令行应用程序以如下形式连续输出YAML数据:
I have a command line app the continuously outputs YAML data in the form:
- col0: datum0
col1: datum1
col2: datum2
- col0: datum0
col1: datum1
col2: datum2
...
这一切都是为了永恒。我想写一个连续读取这些记录的Python脚本。
It does this for all of eternity. I would like to write a Python script that continuously reads each of these records.
PyYAML库似乎最适合采用完全加载的字符串并将其解释为完整的YAML文档。有没有办法让PyYAML进入流模式?
The PyYAML library seems best at taking fully loaded strings and interpreting those as a complete YAML document. Is there a way to put PyYAML into a "streaming" mode?
或者是我唯一的选择自己把数据块化并一个一个地送入PyYAML? p>
Or is my only option to chunk the data myself and feed it bit by bit into PyYAML?
推荐答案
这里是我最后使用,因为似乎没有一个内置的方法来完成我想要的。这个函数应该是通用的,它可以读取一个YAML流,并返回顶级对象,因为他们遇到。
Here is what I've ended up using since there does not seem to be a built-in method for accomplishing what I want. This function should be generic enough that it can read in a stream of YAML and return top-level objects as they are encountered.
def streamInYAML(stream):
y = stream.readline()
cont = 1
while cont:
l = stream.readline()
if len(l) == 0:
cont = 0
else:
if l.startswith(' '):
y = y + l
else:
yield yaml.load(y)
y = l
任何人都能做得更好吗?
Can anyone do better?
这篇关于如何在Python中处理YAML流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!