我想使用ANSI转义序列来控制格式,将二维列表呈现为很好的表格输出。使用IO.ANSI输出表格数据
所以给出这样的数据:
data = [
[ 100, 20, 30 ],
[ 20, 10, 20 ],
[ 50, 400, 20 ]
]
我想输出是这样的:
100 20 30
20 10 20
50 400 20
非常感谢
我想使用ANSI转义序列来控制格式,将二维列表呈现为很好的表格输出。使用IO.ANSI输出表格数据
所以给出这样的数据:
data = [
[ 100, 20, 30 ],
[ 20, 10, 20 ],
[ 50, 400, 20 ]
]
我想输出是这样的:
100 20 30
20 10 20
50 400 20
非常感谢
下面是一个更复杂的解决方案,具有任意宽值的工作原理:
defmodule Table do
def format(rows, opts \\ []) do
padding = Keyword.get(opts, :padding, 1)
rows = stringify(rows)
widths = rows |> transpose |> column_widths
rows |> pad_cells(widths, padding) |> join_rows
end
defp pad_cells(rows, widths, padding) do
Enum.map rows, fn row ->
for {val, width} <- Enum.zip(row, widths) do
String.ljust(val, width + padding)
end
end
end
def join_rows(rows) do
rows |> Enum.map(&Enum.join/1) |> Enum.join("\n")
end
defp stringify(rows) do
Enum.map rows, fn row ->
Enum.map(row, &to_string/1)
end
end
defp column_widths(columns) do
Enum.map columns, fn column ->
column |> Enum.map(&String.length/1) |> Enum.max
end
end
# http://stackoverflow.com/questions/23705074
defp transpose([[]|_]), do: []
defp transpose(rows) do
[Enum.map(rows, &hd/1) | transpose(Enum.map(rows, &tl/1))]
end
end
使用方法如下:
Table.format(data, padding: 2) |> IO.puts
此打印:
100 20 30
20 10 20
50 400 20
您可以用制表符,如果每个项目实现的间距少于7个字符长:
iex> Enum.each(data, fn (x) -> Enum.join(x, "\t") |> IO.puts end)
100 20 30
20 10 20
50 400 20
:ok
Thanks Gazler,我喜欢这个选项的简单性。虽然在我的情况下,每个项目可能会超过7个字符。再次感谢 – b73
在"Programming Elixir" book by Dave Thomas也存在类似的你需要什么运动待办事项:
编写代码来将数据格式化为列,像在本章开始时的输出样本:
# | Created at | Title
-----+----------------------+--------------------------------------------------
2722 | 2014-08-27T04:33:39Z | Added first draft of File.mv for moving files aro
2728 | 2014-08-29T14:28:30Z | Should elixir (and other applications) have stick
-----+----------------------+--------------------------------------------------
,并有a place where readers can post their solutions to this exercise在这里你可以找到和选择什么样的套房,为您。您将需要修改代码,以使其适用于您的输入数据结构(一组数组,这是一个矩阵),但这应该很容易。如果你有任何麻烦,这只是问。
顺便说一句,这里是我的解决方案,而读的书我写道:
defmodule Issues.TableFormatter do
import Enum, only: [map: 2, max: 1, zip: 2, join: 2]
@header ~w(number created_at title)
@header_column_separator "-+-"
# TODO: Refactor; try to find more uses for stdlib functions
def print_table(rows, header \\ @header) do
table = rows |> to_table(header)
header = header |> map(&String.Chars.to_string/1) # the headers needs now to be a string
columns_widths = [header | table] |> columns_widths
hr = for _ <- 1..(length(header)), do: "-"
hr |> print_row(columns_widths, @header_column_separator)
header |> print_row(columns_widths)
hr |> print_row(columns_widths, @header_column_separator)
table |> map &(print_row(&1, columns_widths))
hr |> print_row(columns_widths, @header_column_separator)
end
def to_table(list_of_dicts, header) do
list_of_dicts
|> select_keys(header)
|> map(fn (dict) ->
dict
|> Dict.values
|> map(&String.Chars.to_string/1)
end)
end
def columns_widths(table) do
table
|> Matrix.transpose
|> map(fn (cell) ->
cell
|> map(&String.length/1)
|> max
end)
end
def select_keys(dict, keys) do
for entry <- dict do
{dict1, _} = Dict.split(entry, keys)
dict1
end
end
def print_row(row, column_widths, separator \\ " | ") do
padding = separator |> String.to_char_list |> List.first # Hack
IO.puts row
|> zip(column_widths)
|> map(fn ({ cell, column_width }) ->
String.ljust(cell, column_width, padding)
end)
|> join(separator)
end
end
对待这一切为灵感不是直接解决您的问题。这也可能更符合您的需求,但能够以通用的方式快速格式化某些表格数据并将其打印在终端中,这对您而言可能非常方便。
感谢Simon Jez,那里肯定有一些很好的例子。你提供了一些很好的灵感 - 谢谢。 – b73
有一个Elixir的printf库,如果你不熟悉Erlang字符串格式化,这种事情会更简单。
感谢帕特里克,这是一个非常好的解决方案:) – b73
如果有人有兴趣,我已经在https://gist.github.com/ivan/dc26e349de6f1693c1c77355ec85b643上重写了这段代码,以1)return iodata 2)not pad最后一列3)不会使虚拟机崩溃0行 –