当你在Julia shell中运行时,如果你运行函数zeros(5, 5)
你会得到看起来像这样的东西:
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0
如果你将多维数组存储在变量中,并在shell或外部脚本中打印(或直接打印(,你会得到更丑陋的结果:
[0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0; 0.0 0.0 0.0 0.0 0.0]
有没有办法访问数组的内置 STDOUT 格式化程序,在 shell 中以人类可读的方式显示它?
使用display(x)
而不是print(x)
。
请注意,print(x)
在需要复制粘贴可运行代码的情况下非常有用。
要完成@crstnbr答案,我还建议显示
M=rand(2,3)
f = open("test.txt","w")
show(f, "text/plain", M)
close(f)
然后,如果您阅读并打印测试.txt您将获得:
julia> print(read("test.txt",String))
2×3 Array{Float64,2}:
0.73478 0.184505 0.0678265
0.309209 0.204602 0.831286
注意:除了文件 F,您还可以使用 stdout。
要将某些数据保存在流中,函数显示比显示更适合,如文档(?display(中所述:
In general, you cannot assume that display output goes to stdout (unlike print(x) or show(x)). For example, display(x) may open up a separate window with an image. display(x) means "show x in the best way you can for the current output device(s)." If you want REPL-like text output that is guaranteed to go to stdout, use show(stdout, "text/plain", x) instead.