Arrays 在julia script/print()中使用shell数组输出格式化的多维数组

Arrays 在julia script/print()中使用shell数组输出格式化的多维数组,arrays,multidimensional-array,printf,julia,Arrays,Multidimensional Array,Printf,Julia,在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

在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]
有没有一种方法可以访问数组的内置标准输出格式化程序,并在shell中以人类可读的方式显示它?

使用
display(x)
而不是
print(x)


请注意,
print(x)
在需要复制粘贴可运行代码的情况下非常有用

要完成@crstnbr的回答,我还建议show

M=rand(2,3)
f = open("test.txt","w")
show(f, "text/plain", M)
close(f)
然后,如果您阅读并打印test.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

如文档(?显示)中所述,要在流中保存某些数据,功能显示显示更合适:


是的,提到这一点也是有道理的!相关的:
 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.