我知道这可能非常简单,但我在一个文件中有一些这样的数据:
Artichoke
Green Globe, Imperial Star, Violetto
24" deep
Beans, Lima
Bush Baby, Bush Lima, Fordhook, Fordhook 242
12" wide x 8-10" deep我希望能够格式化成一个很好的TSV类型的表,看起来像这样:
Name | Varieties | Container Data
----------|------------- |-------
some data here nicely padded with even spacing and right aligned text 发布于 2012-06-02 00:32:50
为此,我编写了一个gem:http://tableprintgem.com
发布于 2011-03-07 06:15:27
我有一个小函数来打印一个二维数组作为一个表。每行必须具有相同的列数才能正常工作。它也很容易根据您的需求进行调整。
def print_table(table)
# Calculate widths
widths = []
table.each{|line|
c = 0
line.each{|col|
widths[c] = (widths[c] && widths[c] > col.length) ? widths[c] : col.length
c += 1
}
}
# Indent the last column left.
last = widths.pop()
format = widths.collect{|n| "%#{n}s"}.join(" ")
format += " %-#{last}s\n"
# Print each line.
table.each{|line|
printf format, *line
}
end发布于 2013-11-05 23:49:23
另一个gem:https://github.com/visionmedia/terminal-table终端表是一个用Ruby语言编写的快速、简单、功能丰富的ASCII表生成器。
https://stackoverflow.com/questions/603047
复制相似问题