7

好的,我有一个由一些开源软件输出的表,但它没有以实际的表格式输出,例如

<table> 
  <thead>
     <td>Heading</td>
  <thead>
  <tbody>
    <tr>
       <td>Content</td>
    </tr>
  <tbody>
</table

相反,开发该软件的人认为像这样输出表格是个好主意

+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+

所以我不能建立一个网络刮板来获取数据,或者我不知道我是否可以建立一个刮板来刮掉它,因为它全部包裹在一个<pre> </pre>标签内。因此,相反,我一直在尝试使用 ruby​​ 和 Regex 来尝试完成工作,到目前为止,我已经设法获得了所有领先|的信息,而且我也设法获得了标题,+-------+-----但到目前为止,我似乎已经一直重复该模式,它不想重复自己,好吧但是现在说够了这是我到目前为止使用的代码

text.lines.to_a.each do |line|
   line.sub(/^\| |^\+*-*\+*\-*/) do |match|
    puts "Regexp Match: " << match
end
STDIN.getc
puts "New Line "<< line
end

例如,第一行的输出只能是+-----------------+---------- CSV 格式,因此可以用 'sGsub替换剩余|,'s

我可以使用 PHP 或 Ruby,所以任何答案都非常受欢迎

4

4 回答 4

3

这可能不像现在那样干净,但它适用于这个例子:) Ruby:

@text = <<END
+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+
END
s = @text.scan(/^[|]\W(.*)[|]$/)
puts s
arr = []
arr2 = []
s.each do |o|
  a = o.to_s.split('|')
    a.each do |oo|
      arr2 << oo.to_s.gsub('["','').gsub('"]','').gsub(/\s+/, "")
    end
    arr << arr2
  arr2 = []
end
arr.each do |i|
  puts i
end
于 2013-02-26T10:04:49.383 回答
2

查看:

$table = '+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+';

$lines = preg_split('/\r\n|\r|\n/', $table);
$array = array();
foreach($lines as $line){
  if(!preg_match('/\+-+\+/', $line)){
    $array[] = preg_split('/\s*\|\s*/', trim($line, '| '));
  }
}

print_r($array);

输出:

Array
(
    [0] => Array
        (
            [0] => HEADING 1
            [1] => HEADING 2
            [2] => ETC
            [3] => ANOTHER
            [4] => HEADING3
            [5] => HEADING4
            [6] => SML
        )

    [1] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [2] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [3] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [4] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [5] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [6] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [7] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [8] => Array
        (
            [0] => content
            [1] => more content
            [2] => cont
            [3] => More more
            [4] => content
            [5] => content 2.0
            [6] => litl
        )

    [9] => Array
        (
            [0] => TOTALS        AGENTS:21
            [1] => total
            [2] => total
            [3] => total
            [4] => total
            [5] => total
        )

)

希望这有帮助:)

于 2013-02-26T08:38:03.133 回答
2

这是一个完整的红宝石解决方案。不过,您需要手动将 a 添加|到最后一行。

require 'builder'

table = '+------------+-------------+-------+-------------+------------+---------------+----------+
| HEADING 1  | HEADING 2   | ETC   | ANOTHER     | HEADING3   | HEADING4     | SML |
+------------+-------------+-------+-------------+------------+---------------+----------+
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
| content   | more content | cont  | More more   | content    | content 2.0  | litl |
+------------+-------------+-------+-------------+------------+--------------+----------+
| TOTALS        AGENTS:21  |  total|        total|       total|         total| total|
+------------+-------------+-------+-------------+------------+--------------+----------+';

def parse_table(table)
  rows = []
  table.each_line do |line|
    next if line.match /^\+/
    rows << line.split(/\s*\|\s*/).reject(&:empty?) 
  end
  rows
end

def html_row(xml, columns)
  xml.tr do
    columns.each do |column|
      xml.td column
    end
  end
end

def html_table(rows)
  head_row = rows.first
  body_rows = rows[1..-1]

  xml = Builder::XmlMarkup.new :indent => 2
  xml.table do
    xml.thead do
      html_row xml, head_row
    end
    xml.tbody do
      body_rows.each do |body_row|
        html_row xml, body_row
      end
    end
  end.to_s
end


rows = parse_table(table)
html = html_table(rows)
puts html

输出:

<table>
  <thead>
    <tr>
      <td>HEADING 1</td>
      <td>HEADING 2</td>
      <td>ETC</td>
      <td>ANOTHER</td>
      <td>HEADING3</td>
      <td>HEADING4</td>
      <td>SML</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>content</td>
      <td>more content</td>
      <td>cont</td>
      <td>More more</td>
      <td>content</td>
      <td>content 2.0</td>
      <td>litl</td>
    </tr>
    <tr>
      <td>TOTALS        AGENTS:21</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
      <td>total</td>
    </tr>
  </tbody>
</table>
于 2013-02-26T09:41:31.400 回答
0

对于将字段从表中取出的主要工作,使用split模式来获取每一行:

line.split(/\s*\|\s*/)

|这将根据每个和任何周围的空白将该行拆分为一个数组。丢弃数组的第一个和最后一个元素,因为该模式也匹配开始和结束|

于 2013-02-26T08:29:45.440 回答