将变量的 Julia REPL 输出作为字符串获取
Get Julia REPL output of variable as a string
我想得到像在 Julia REPL 中作为字符串打印的输出,而不是打印到 REPL。
考虑到您有一个大矩阵 x
。
x = rand(100, 100)
在 REPL 中,x 以一种很好的方式显示。我想通过函数调用将此输出作为字符串获取(不是以 hacky 方式)。我该怎么做?
我尝试使用函数 repr
来获得输出:
repr(x)
这给出了一个很长的字符串,包含所有数字并使屏幕混乱。
我尝试使用 context
参数。
首先使用 displaysize
键:
repr(x, context = :displaysize => (80,80))
没有任何效果。 (?)
我尝试使用 limit
键:
repr(x, context = :limit => true)
这提供了一个不再使屏幕混乱的输出,但它看起来不像“正常”REPL 输出那么好。
我对 print
函数和 IOContext
进行了相同的尝试
io = IOBuffer();
print(IOContext(io, :limit => true), x)
String(take!(io))
这给出了与 repr
相同的结果。
我想这就是你想要的:
julia> x=rand(100,100);
julia> io = IOBuffer();
julia> show(IOContext(io, :limit => true, :displaysize => (10, 10)), "text/plain", x);
julia> s = String(take!(io));
julia> println(s)
100×100 Array{Float64,2}:
0.150112 … 0.913
0.14348 0.598862
0.265236 0.378648
⋮ ⋱
0.599803 0.778466
0.79655 0.725736
julia> io = IOBuffer();
julia> show(IOContext(io, :limit => true, :displaysize => (20, 40)), "text/plain", x);
julia> s = String(take!(io));
julia> println(s)
100×100 Array{Float64,2}:
0.150112 0.998585 … 0.913
0.14348 0.754191 0.598862
0.265236 0.364718 0.378648
0.746999 0.436714 0.594933
0.247191 0.340162 0.126489
0.92214 0.518781 … 0.530581
0.483844 0.146089 0.14216
0.421205 0.401629 0.381202
⋮ ⋱
0.0944732 0.990715 0.132398
0.711658 0.958458 0.0849586
0.378591 0.518736 … 0.688399
0.77595 0.319994 0.667458
0.427935 0.375477 0.656718
0.599803 0.779445 0.778466
0.79655 0.939409 0.725736
这里的两个关键点是:
- 你使用
show
并指定你想要使用的 MIME(我猜你想要 "text/plain"
,它在 REPL 中给出;一般来说,例如在 Jupyter Notebook 内部 HTML 和 LaTeX 是用于某些类型,例如,这是我们在 DataFrames.jl 中用来显示 DataFrame
个对象)
- 你通常也应该设置
:displaysize
,通常 show
从 stdout
获取显示大小,但如果你有自定义 io
那么它不知道有多少您可能想要显示的行和列(当然,如果您喜欢它们,您可以坚持使用默认值:))
我想得到像在 Julia REPL 中作为字符串打印的输出,而不是打印到 REPL。
考虑到您有一个大矩阵 x
。
x = rand(100, 100)
在 REPL 中,x 以一种很好的方式显示。我想通过函数调用将此输出作为字符串获取(不是以 hacky 方式)。我该怎么做?
我尝试使用函数 repr
来获得输出:
repr(x)
这给出了一个很长的字符串,包含所有数字并使屏幕混乱。
我尝试使用 context
参数。
首先使用 displaysize
键:
repr(x, context = :displaysize => (80,80))
没有任何效果。 (?)
我尝试使用 limit
键:
repr(x, context = :limit => true)
这提供了一个不再使屏幕混乱的输出,但它看起来不像“正常”REPL 输出那么好。
我对 print
函数和 IOContext
io = IOBuffer();
print(IOContext(io, :limit => true), x)
String(take!(io))
这给出了与 repr
相同的结果。
我想这就是你想要的:
julia> x=rand(100,100);
julia> io = IOBuffer();
julia> show(IOContext(io, :limit => true, :displaysize => (10, 10)), "text/plain", x);
julia> s = String(take!(io));
julia> println(s)
100×100 Array{Float64,2}:
0.150112 … 0.913
0.14348 0.598862
0.265236 0.378648
⋮ ⋱
0.599803 0.778466
0.79655 0.725736
julia> io = IOBuffer();
julia> show(IOContext(io, :limit => true, :displaysize => (20, 40)), "text/plain", x);
julia> s = String(take!(io));
julia> println(s)
100×100 Array{Float64,2}:
0.150112 0.998585 … 0.913
0.14348 0.754191 0.598862
0.265236 0.364718 0.378648
0.746999 0.436714 0.594933
0.247191 0.340162 0.126489
0.92214 0.518781 … 0.530581
0.483844 0.146089 0.14216
0.421205 0.401629 0.381202
⋮ ⋱
0.0944732 0.990715 0.132398
0.711658 0.958458 0.0849586
0.378591 0.518736 … 0.688399
0.77595 0.319994 0.667458
0.427935 0.375477 0.656718
0.599803 0.779445 0.778466
0.79655 0.939409 0.725736
这里的两个关键点是:
- 你使用
show
并指定你想要使用的 MIME(我猜你想要"text/plain"
,它在 REPL 中给出;一般来说,例如在 Jupyter Notebook 内部 HTML 和 LaTeX 是用于某些类型,例如,这是我们在 DataFrames.jl 中用来显示DataFrame
个对象) - 你通常也应该设置
:displaysize
,通常show
从stdout
获取显示大小,但如果你有自定义io
那么它不知道有多少您可能想要显示的行和列(当然,如果您喜欢它们,您可以坚持使用默认值:))