1

What's the best way to save a dictionary to a file, so that I can load it later in Tcl on a different computer/system?

I don't think fconfigure $stream -translation binary; puts -nonewline $stream $dict works if there's keys/values with unicode characters > \u00ff. Is "utf-8" encoding OK (to save disk space), or should I always use full "unicode"?

Dictionaries are new to me, but since their textual representation is that of a list with alternating keys/values, and a list is just a string with some extra syntax characters, maybe the question could've been "Safely save a string to file"?

4

1 回答 1

2

您的问题可以简化:

我应该使用什么编码?

答案是:视情况而定。

  • 如果你只有二进制数据,那么二进制是要走的路。
  • 如果您主要有 ascii 字符和一些例外(如变音符号等),那么 utf-8 是最适合您的编码(字符之外的字符\x00\x7f2 个或更多字节编码,ascii 用 1 个字节编码)。
  • 如果您有很多CJK,那么使用 unicode 或其他一些编码可能会更好(每个字符 2 字节 vs 3 字节)

没错:列表和字典的(反)序列化在 Tcl 中比在许多其他语言中容易得多:将其保存为字符串。

于 2013-03-20T11:23:10.403 回答