Julia写数据文件的时候如何使数据左对齐


#1

我想将一个100×4的矩阵写入一个TXT文件,矩阵元素的小数位数各不相同,我想让他们左对齐输出,生成整整齐齐的4列,请问怎样可以做到


#2

using DelimitedFiles

writedlm(f, A, delim=’\t’)


#3

源代码
using DelimitedFiles
a = rand(10 , 4)
f = open(“delim_file.txt” , “w”)
writedlm(f , a , delm = ‘\t’)
报错:
ERROR: LoadError: ArgumentError: unknown option delmStacktrace:
[1] val_opts(::Base.Iterators.Pairs{Symbol,Char,Tuple{Symbol},NamedTuple{(:delm,),Tuple{Char}}}) at C:\Users\julia\AppData\Local\Julia-1.2.0\share\julia\stdlib\v1.2\DelimitedFiles\src\DelimitedFiles.jl:482
[2] #writedlm#12(::Base.Iterators.Pairs{Symbol,Char,Tuple{Symbol},NamedTuple{(:delm,),Tuple{Char}}}, ::typeof(writedlm), ::IOStream, ::Array{Float64,2}, ::Char) at C:\Users\julia\AppData\Local\Julia-1.2.0\share\julia\stdlib\v1.2\DelimitedFiles\src\DelimitedFiles.jl:737
[3] #writedlm at .\none:0 [inlined]
[4] #writedlm#18 at C:\Users\julia\AppData\Local\Julia-1.2.0\share\julia\stdlib\v1.2\DelimitedFiles\src\DelimitedFiles.jl:826 [inlined]
[5] (::getfield(DelimitedFiles, Symbol("#kw##writedlm")))(::NamedTuple{(:delm,),Tuple{Char}}, ::typeof(writedlm), ::IOStream, ::Array{Float64,2}) at .\none:0
[6] top-level scope at D:\Julia\ex5.jl:4
in expression starting at D:\Julia\ex5.jl:4


#4
a = rand(10, 4)
writedlm("delim_file", a) # delim不是关键词参数

不过这个不是左对齐,如果想完全控制对齐,建议用Printf中的@printf一行一行的输出。