我有一个正在捕获一些数据的 tcl 脚本。我想将此数据写入 YAML 文件。我可以阅读有关如何在 TCL 中执行此操作的任何示例或地方吗?
Tcllib 中的yaml包可以做到这一点。
如果您的数据是一个简单的单级字典,您可以这样做:
package require yaml
set yamlText [yaml::dict2yaml $yourDictionary]
Run Code Online (Sandbox Code Playgroud)
例如:
% 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
%
Run Code Online (Sandbox Code Playgroud)
这样做的主要问题是它假设每个键中的所有内容实际上都是一个字符串(尽管数字通常足够短,结果也可以)。
因此,如果您的数据更复杂,您需要首先使用huddle包构建它的混乱描述。这嵌入了生成复杂结构所需的额外类型信息。
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
Run Code Online (Sandbox Code Playgroud)