1

我有一个 Excel 电子表格,其中包含许多人对另一个人的身高和体重的估计。此外,有些人在这两个估算单元格上都留下了评论,例如“此估算考虑了某某”。

我想从电子表格中获取数据(我已经想出了如何解析它),并将其表示为纯文本文件,以便我可以轻松地将其解析回结构化格式(理想情况下使用 Perl)。

最初我想使用 YAML:

Tom:
  Height:
    Estimate: 5
    Comment: Not that confident
  Weight:
    Estimate: 7
    Comment: Very confident
Natalia: ...

但是现在我认为这有点难以阅读,我想知道是否有一些文本表格表示会更容易阅读并且仍然可以解析。

就像是:

PERSON      HEIGHT     Weight
-----------------------------
Tom         5          7
___START_HEIGHT_COMMENT___
    We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.  That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed [...]  
Wait, what's this project about again?
___END_HEIGHT_COMMENT___
___START_WEIGHT_COMMENT___
    We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.  That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed [...]  
Wait, what's this project about again?
___END_WEIGHT_COMMENT___

Natalia     2          4
John        3          3

有一个更好的方法吗?

4

5 回答 5

3

CSV(逗号分隔值)。

您甚至可以将其从 Excel 中直接保存为这种格式,并从这种格式中直接将其读入 Excel。然而,它也是人类可读的,并且易于机器解析。

于 2009-05-29T23:18:18.607 回答
1

通常,如果我想以文本形式从电子表格中捕获数据,我会使用 CSV(Excel 可以读取和写入)。它易于生成和解析,并且与许多其他工具兼容,但它在“人类可读”图表上的排名并不高。它可以被读取,但除了具有相等字段宽度的简单文件之外,它对于其他任何东西都很尴尬。

XML 是一种选择,但 YAML 更易于阅读。可读性是 YAML 的设计目标之一。YAML::Tiny模块是一个很好的轻量级模块,适用于典型案例。

看起来您想到的是纯文本表格,或者可能是带有固定列的表格格式。CPAN 上有一些可能有用的模块:Text::TableText::SimpleTable其他......这些模块可以生成易于阅读但解析它的表示形式。(它们旨在用于数据表示,而不是存储和检索。)您可能必须构建自己的解析器。

于 2009-05-30T02:10:21.757 回答
0

还有用于简单数据的Config::General及其相关类系列。

于 2009-06-02T19:18:44.270 回答
0

添加到罗伯特的答案中,您可以简单地将评论放在其他列中(逗号将被 Excel 的 CSV 输出过滤器等转义)。有关 CSV 格式的更多信息:www.csvreader.com/csv_format.php

于 2009-05-29T23:41:38.670 回答
0

没有理由不能使用 XML,尽管我认为在这种特殊情况下它是矫枉过正的。

于 2009-05-30T00:11:36.893 回答