简体   繁体   中英

Is there a parser for the output from Perl's Text::Table?

Suppose I have a bunch of tables created with Text::Table . Does there exist a parser to convert them back to Perl data structures, or do I have to write my own?

Text::Table is a module for creating data presentations. If it were intended for storage and retrieval (ie a file format) it would include methods for parsing existing tables.

It seems somewhat convoluted. If you had the information before converting it into a table, then why try to parse it from its presentation form? It's like having a text file, converting it to latex, then to postscript, and then trying to get the text back from the postscript file.

I'm sure there's a way to parse the output of Text::Table, but it seems that your workflow is flawed; I'd aim at using a simpler output for the data (besides Text::Table, if you really have to have it that way) like YAML that can then be trivially restored to the original data structure.

通过在Google上执行“ site:search.cpan.org“ Text :: Table” parse“分析,我没有找到用于此目的的模块。

Why not use CSV?

CSV isn't exactly great at being human readable if you have many columns of varying lengths, but it can be done in a pinch.

The good thing about CSV is that any spreadsheet (as well as many other tools) will parse and display CSV in a nice, friendly way.

Update

Brian,

Based on this comment by the OP, above:

The customer wants a tabular data format that's both human and machine readable. – Horace Loeb May 31 at 3:26

I drew the conclusion that he had control over the producing code as a the consuming code.

My answer is really answering the question "What format can I use that is both human and machine readable?"

You could persist them as the Perl selves before you output it. That way whatever you printed once, could be reprinted on demand. See Storable --or YAML::Syck , which would dump it out in YAML.

Once you has a format for Storable , you might

  1. Read the file in
  2. Have some standard format to check for requested changes.
  3. Write it back out if changed.

However, with YAML, it might be easy enough to just modify the YAML file.

The YAML for the example given for Text::Table , looks like this:

--- !!perl/hash:Text::Table 
blank: ~
cols: 
  - 
    - Mercury
    - Venus
    - Earth
    - Jupiter
  - 
    - 2360
    - 6110
    - 6378
    - 71030
  - 
    - 3.7
    - 5.1
    - 5.52
    - 1.3
forms: 
  - "%s %s %s"
  - "%s %s %s"
lines: ~
spec: 
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Planet
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Radius
      - km
  - 
    align: auto
    align_title: left
    align_title_lines: left
    sample: []

    title: 
      - Density
      - g/cm^3
titles: 
  - 
    - Planet
    - "      "
  - 
    - Radius
    - "km    "
  - 
    - Density
    - "g/cm^3 "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM