TCL将数据写入YAML文件

时间:2016-06-25 22:58:26

标签: tcl

我有一个捕获一些数据的tcl脚本。我想将此数据写入YAML文件。有没有关于如何在TCL中做到这一点的例子或地方?

2 个答案:

答案 0 :(得分:2)

Tcllib中的yaml package可以做到。

如果您的数据是一个简单的单级词典,就可以这样做:

package require yaml

set yamlText [yaml::dict2yaml $yourDictionary]

例如:

% set d {foo 123 bar "this is a piece of text"}
foo 123 bar "this is a piece of text"
% yaml::dict2yaml $d
---
foo: 123
bar: this is a piece of text

% 

这个问题的主要问题在于它假设每个键中的所有内容都是一个字符串(虽然数字通常很短,但也可以出来)。

因此,如果您的数据更复杂,您需要首先使用huddle package构建对它的简要描述。这嵌入了生成复杂结构所需的额外类型信息。

package require huddle
package require yaml

set inner [huddle create boo "this is some text" grill "this is some other text"]
set outer [huddle create foo 123 bar $inner]
yaml::huddle2yaml $outer

答案 1 :(得分:0)

标准Tcl库(tcllib)中有一个yaml包。如果该包不能满足您的需求,您可以查看想法的来源。

相关问题