Delimited Files

DelimitedFiles.readdlm — Method.

  1. 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

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = [5; 6; 7; 8];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y])
  6. end
  7. julia> readdlm("delim_file.txt", '\t', Int, '\n')
  8. 4×2 Array{Int64,2}:
  9. 1 5
  10. 2 6
  11. 3 7
  12. 4 8

DelimitedFiles.readdlm — Method.

  1. 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.

  1. readdlm(source, delim::AbstractChar, T::Type; options...)

The end of line delimiter is taken as \n.

Examples

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = [1.1; 2.2; 3.3; 4.4];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y], ',')
  6. end;
  7. julia> readdlm("delim_file.txt", ',', Float64)
  8. 4×2 Array{Float64,2}:
  9. 1.0 1.1
  10. 2.0 2.2
  11. 3.0 3.3
  12. 4.0 4.4
  13. julia> rm("delim_file.txt")

DelimitedFiles.readdlm — Method.

  1. 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

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = [1.1; 2.2; 3.3; 4.4];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y], ',')
  6. end;
  7. julia> readdlm("delim_file.txt", ',')
  8. 4×2 Array{Float64,2}:
  9. 1.0 1.1
  10. 2.0 2.2
  11. 3.0 3.3
  12. 4.0 4.4
  13. julia> rm("delim_file.txt")
  14. julia> z = ["a"; "b"; "c"; "d"];
  15. julia> open("delim_file.txt", "w") do io
  16. writedlm(io, [x z], ',')
  17. end;
  18. julia> readdlm("delim_file.txt", ',')
  19. 4×2 Array{Any,2}:
  20. 1 "a"
  21. 2 "b"
  22. 3 "c"
  23. 4 "d"
  24. julia> rm("delim_file.txt")

DelimitedFiles.readdlm — Method.

  1. 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

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = [5; 6; 7; 8];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y])
  6. end;
  7. julia> readdlm("delim_file.txt", Int64)
  8. 4×2 Array{Int64,2}:
  9. 1 5
  10. 2 6
  11. 3 7
  12. 4 8
  13. julia> readdlm("delim_file.txt", Float64)
  14. 4×2 Array{Float64,2}:
  15. 1.0 5.0
  16. 2.0 6.0
  17. 3.0 7.0
  18. 4.0 8.0
  19. julia> rm("delim_file.txt")

DelimitedFiles.readdlm — Method.

  1. 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

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = ["a"; "b"; "c"; "d"];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y])
  6. end;
  7. julia> readdlm("delim_file.txt")
  8. 4×2 Array{Any,2}:
  9. 1 "a"
  10. 2 "b"
  11. 3 "c"
  12. 4 "d"
  13. julia> rm("delim_file.txt")

DelimitedFiles.writedlm — Function.

  1. 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

  1. julia> using DelimitedFiles
  2. julia> x = [1; 2; 3; 4];
  3. julia> y = [5; 6; 7; 8];
  4. julia> open("delim_file.txt", "w") do io
  5. writedlm(io, [x y])
  6. end
  7. julia> readdlm("delim_file.txt", '\t', Int, '\n')
  8. 4×2 Array{Int64,2}:
  9. 1 5
  10. 2 6
  11. 3 7
  12. 4 8
  13. julia> rm("delim_file.txt")

原文: https://juliacn.github.io/JuliaZH.jl/latest/stdlib/DelimitedFiles/