Delimited Files
DelimitedFiles.readdlm
— Method.
readdlm(source, delim::AbstractChar, T::Type, eol::AbstractChar; header=false, skipstart=0, skipblanks=true, use_mmap, quotes=true, dims, comments=false, comment_char='#')
Read a matrix from the source where each line (separated by eol
) gives one row, with elements separated by the given delimiter. The source can be a text file, stream or byte array. Memory mapped files can be used by passing the byte array representation of the mapped segment as source.
If T
is a numeric type, the result is an array of that type, with any non-numeric elements as NaN
for floating-point types, or zero. Other useful values of T
include String
, AbstractString
, and Any
.
If header
is true
, the first row of data will be read as header and the tuple (data_cells, header_cells)
is returned instead of only data_cells
.
Specifying skipstart
will ignore the corresponding number of initial lines from the input.
If skipblanks
is true
, blank lines in the input will be ignored.
If use_mmap
is true
, the file specified by source
is memory mapped for potential speedups. Default is true
except on Windows. On Windows, you may want to specify true
if the file is large, and is only read once and not written to.
If quotes
is true
, columns enclosed within double-quote (") characters are allowed to contain new lines and column delimiters. Double-quote characters within a quoted field must be escaped with another double-quote. Specifying dims
as a tuple of the expected rows and columns (including header, if any) may speed up reading of large files. If comments
is true
, lines beginning with comment_char
and text following comment_char
in any line are ignored.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [5; 6; 7; 8];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y])
end
julia> readdlm("delim_file.txt", '\t', Int, '\n')
4×2 Array{Int64,2}:
1 5
2 6
3 7
4 8
DelimitedFiles.readdlm
— Method.
readdlm(source, delim::AbstractChar, eol::AbstractChar; options...)
If all data is numeric, the result will be a numeric array. If some elements cannot be parsed as numbers, a heterogeneous array of numbers and strings is returned.
DelimitedFiles.readdlm
— Method.
readdlm(source, delim::AbstractChar, T::Type; options...)
The end of line delimiter is taken as \n
.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [1.1; 2.2; 3.3; 4.4];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y], ',')
end;
julia> readdlm("delim_file.txt", ',', Float64)
4×2 Array{Float64,2}:
1.0 1.1
2.0 2.2
3.0 3.3
4.0 4.4
julia> rm("delim_file.txt")
DelimitedFiles.readdlm
— Method.
readdlm(source, delim::AbstractChar; options...)
The end of line delimiter is taken as \n
. If all data is numeric, the result will be a numeric array. If some elements cannot be parsed as numbers, a heterogeneous array of numbers and strings is returned.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [1.1; 2.2; 3.3; 4.4];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y], ',')
end;
julia> readdlm("delim_file.txt", ',')
4×2 Array{Float64,2}:
1.0 1.1
2.0 2.2
3.0 3.3
4.0 4.4
julia> rm("delim_file.txt")
julia> z = ["a"; "b"; "c"; "d"];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x z], ',')
end;
julia> readdlm("delim_file.txt", ',')
4×2 Array{Any,2}:
1 "a"
2 "b"
3 "c"
4 "d"
julia> rm("delim_file.txt")
DelimitedFiles.readdlm
— Method.
readdlm(source, T::Type; options...)
The columns are assumed to be separated by one or more whitespaces. The end of line delimiter is taken as \n
.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [5; 6; 7; 8];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y])
end;
julia> readdlm("delim_file.txt", Int64)
4×2 Array{Int64,2}:
1 5
2 6
3 7
4 8
julia> readdlm("delim_file.txt", Float64)
4×2 Array{Float64,2}:
1.0 5.0
2.0 6.0
3.0 7.0
4.0 8.0
julia> rm("delim_file.txt")
DelimitedFiles.readdlm
— Method.
readdlm(source; options...)
The columns are assumed to be separated by one or more whitespaces. The end of line delimiter is taken as \n
. If all data is numeric, the result will be a numeric array. If some elements cannot be parsed as numbers, a heterogeneous array of numbers and strings is returned.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = ["a"; "b"; "c"; "d"];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y])
end;
julia> readdlm("delim_file.txt")
4×2 Array{Any,2}:
1 "a"
2 "b"
3 "c"
4 "d"
julia> rm("delim_file.txt")
DelimitedFiles.writedlm
— Function.
writedlm(f, A, delim='\t'; opts)
Write A
(a vector, matrix, or an iterable collection of iterable rows) as text to f
(either a filename string or an IO
stream) using the given delimiter delim
(which defaults to tab, but can be any printable Julia object, typically a Char
or AbstractString
).
For example, two vectors x
and y
of the same length can be written as two columns of tab-delimited text to f
by either writedlm(f, [x y])
or by writedlm(f, zip(x, y))
.
Examples
julia> using DelimitedFiles
julia> x = [1; 2; 3; 4];
julia> y = [5; 6; 7; 8];
julia> open("delim_file.txt", "w") do io
writedlm(io, [x y])
end
julia> readdlm("delim_file.txt", '\t', Int, '\n')
4×2 Array{Int64,2}:
1 5
2 6
3 7
4 8
julia> rm("delim_file.txt")
原文: https://juliacn.github.io/JuliaZH.jl/latest/stdlib/DelimitedFiles/