diff --git a/NEWS.md b/NEWS.md index b635e507fd78e..e838a3ed0e800 100644 --- a/NEWS.md +++ b/NEWS.md @@ -284,6 +284,9 @@ Deprecated or removed * The method `srand(rng, filename, n=4)` has been deprecated ([#21359]). + * `readcsv(io[, T::Type]; opts...)` has been deprecated in favor of + `readdlm(io, ','[, T]; opts...)` ([#23530]). + * The `cholfact`/`cholfact!` methods that accepted an `uplo` symbol have been deprecated in favor of using `Hermitian` (or `Symmetric`) views ([#22187], [#22188]). diff --git a/base/datafmt.jl b/base/datafmt.jl index 04525ae9ad158..6ceecd14acf40 100644 --- a/base/datafmt.jl +++ b/base/datafmt.jl @@ -6,7 +6,7 @@ module DataFmt import Base: _default_delims, tryparse_internal, show -export countlines, readdlm, readcsv, writedlm +export countlines, readdlm, writedlm invalid_dlm(::Type{Char}) = reinterpret(Char, 0xfffffffe) invalid_dlm(::Type{UInt8}) = 0xfe @@ -616,14 +616,6 @@ function dlm_parse(dbuff::String, eol::D, dlm::D, qchar::D, cchar::D, return (nrows, ncols) end -""" - readcsv(source, [T::Type]; options...) - -Equivalent to [`readdlm`](@ref) with `delim` set to comma, and type optionally defined by `T`. -""" -readcsv(io; opts...) = readdlm(io, ','; opts...) -readcsv(io, T::Type; opts...) = readdlm(io, ',', T; opts...) - # todo: keyword argument for # of digits to print writedlm_cell(io::IO, elt::AbstractFloat, dlm, quotes) = print_shortest(io, elt) function writedlm_cell(io::IO, elt::AbstractString, dlm::T, quotes::Bool) where T diff --git a/base/deprecated.jl b/base/deprecated.jl index 661276c56569c..65d99d763ac0f 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1735,6 +1735,10 @@ function IOContext(io::IO; kws...) IOContext(io, (k=>v for (k, v) in kws)...) end +# deprecate readcsv +@deprecate readcsv(io; opts...) readdlm(io, ','; opts...) +@deprecate readcsv(io, T::Type; opts...) readdlm(io, ',', T; opts...) + @deprecate IOContext(io::IO, key, value) IOContext(io, key=>value) # PR #23485 diff --git a/base/exports.jl b/base/exports.jl index 24548821f7c92..0b359c200e64a 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1049,7 +1049,6 @@ export readavailable, readbytes!, readchomp, - readcsv, readdir, readdlm, readline, diff --git a/doc/src/manual/packages.md b/doc/src/manual/packages.md index ea024571f6f32..19a6f714a1cc5 100644 --- a/doc/src/manual/packages.md +++ b/doc/src/manual/packages.md @@ -811,7 +811,7 @@ the directory of the current source file. For example if `FooBar/src/FooBar.jl` ```julia datapath = joinpath(@__DIR__, "..", "data") -foo = readcsv(joinpath(datapath, "foo.csv")) +foo = readdlm(joinpath(datapath, "foo.csv"), ',') ``` ### Making Your Package Available diff --git a/doc/src/stdlib/io-network.md b/doc/src/stdlib/io-network.md index 931cb3b55bc0d..22dc207996886 100644 --- a/doc/src/stdlib/io-network.md +++ b/doc/src/stdlib/io-network.md @@ -83,7 +83,6 @@ Base.DataFmt.readdlm(::Any, ::Char) Base.DataFmt.readdlm(::Any, ::Type) Base.DataFmt.readdlm(::Any) Base.DataFmt.writedlm -Base.DataFmt.readcsv Base.Base64.Base64EncodePipe Base.Base64.Base64DecodePipe Base.Base64.base64encode diff --git a/test/datafmt.jl b/test/datafmt.jl index 95f0959e7b8ea..d5bdc45495466 100644 --- a/test/datafmt.jl +++ b/test/datafmt.jl @@ -18,18 +18,18 @@ isequaldlm(m1, m2, t) = isequal(m1, m2) && (eltype(m1) == eltype(m2) == t) @test isequaldlm(readdlm(IOBuffer("1\t2\n3\t4\n5\t6\n")), [1. 2; 3 4; 5 6], Float64) @test isequaldlm(readdlm(IOBuffer("1\t2\n3\t4\n5\t6\n"), Int), [1 2; 3 4; 5 6], Int) -@test size(readcsv(IOBuffer("1,2,3,4"))) == (1,4) -@test size(readcsv(IOBuffer("1,2,3,"))) == (1,4) -@test size(readcsv(IOBuffer("1,2,3,4\n"))) == (1,4) -@test size(readcsv(IOBuffer("1,2,3,\n"))) == (1,4) -@test size(readcsv(IOBuffer("1,2,3,4\n1,2,3,4"))) == (2,4) -@test size(readcsv(IOBuffer("1,2,3,4\n1,2,3,"))) == (2,4) -@test size(readcsv(IOBuffer("1,2,3,4\n1,2,3"))) == (2,4) - -@test size(readcsv(IOBuffer("1,2,3,4\r\n"))) == (1,4) -@test size(readcsv(IOBuffer("1,2,3,4\r\n1,2,3\r\n"))) == (2,4) -@test size(readcsv(IOBuffer("1,2,3,4\r\n1,2,3,4\r\n"))) == (2,4) -@test size(readcsv(IOBuffer("1,2,3,\"4\"\r\n1,2,3,4\r\n"))) == (2,4) +@test size(readdlm(IOBuffer("1,2,3,4"), ',')) == (1,4) +@test size(readdlm(IOBuffer("1,2,3,"), ',')) == (1,4) +@test size(readdlm(IOBuffer("1,2,3,4\n"), ',')) == (1,4) +@test size(readdlm(IOBuffer("1,2,3,\n"), ',')) == (1,4) +@test size(readdlm(IOBuffer("1,2,3,4\n1,2,3,4"), ',')) == (2,4) +@test size(readdlm(IOBuffer("1,2,3,4\n1,2,3,"), ',')) == (2,4) +@test size(readdlm(IOBuffer("1,2,3,4\n1,2,3"), ',')) == (2,4) + +@test size(readdlm(IOBuffer("1,2,3,4\r\n"), ',')) == (1,4) +@test size(readdlm(IOBuffer("1,2,3,4\r\n1,2,3\r\n"), ',')) == (2,4) +@test size(readdlm(IOBuffer("1,2,3,4\r\n1,2,3,4\r\n"), ',')) == (2,4) +@test size(readdlm(IOBuffer("1,2,3,\"4\"\r\n1,2,3,4\r\n"), ',')) == (2,4) @test size(readdlm(IOBuffer("1 2 3 4\n1 2 3"))) == (2,4) @test size(readdlm(IOBuffer("1\t2 3 4\n1 2 3"))) == (2,4) @@ -73,11 +73,11 @@ let result1 = reshape(Any["t", "c", "", "c"], 2, 2), @test isequaldlm(readdlm(IOBuffer("t t \n\"\"\"c\" c")), result2, Any) end -@test isequaldlm(readcsv(IOBuffer("\n1,2,3\n4,5,6\n\n\n"), skipblanks=false), +@test isequaldlm(readdlm(IOBuffer("\n1,2,3\n4,5,6\n\n\n"), ',', skipblanks=false), reshape(Any["",1.0,4.0,"","","",2.0,5.0,"","","",3.0,6.0,"",""], 5, 3), Any) -@test isequaldlm(readcsv(IOBuffer("\n1,2,3\n4,5,6\n\n\n"), skipblanks=true), reshape([1.0,4.0,2.0,5.0,3.0,6.0], 2, 3), Float64) -@test isequaldlm(readcsv(IOBuffer("1,2\n\n4,5"), skipblanks=false), reshape(Any[1.0,"",4.0,2.0,"",5.0], 3, 2), Any) -@test isequaldlm(readcsv(IOBuffer("1,2\n\n4,5"), skipblanks=true), reshape([1.0,4.0,2.0,5.0], 2, 2), Float64) +@test isequaldlm(readdlm(IOBuffer("\n1,2,3\n4,5,6\n\n\n"), ',', skipblanks=true), reshape([1.0,4.0,2.0,5.0,3.0,6.0], 2, 3), Float64) +@test isequaldlm(readdlm(IOBuffer("1,2\n\n4,5"), ',', skipblanks=false), reshape(Any[1.0,"",4.0,2.0,"",5.0], 3, 2), Any) +@test isequaldlm(readdlm(IOBuffer("1,2\n\n4,5"), ',', skipblanks=true), reshape([1.0,4.0,2.0,5.0], 2, 2), Float64) let x = bitrand(5, 10), io = IOBuffer() writedlm(io, x) @@ -88,7 +88,7 @@ end let x = [1,2,3], y = [4,5,6], io = IOBuffer() writedlm(io, zip(x,y), ", ") seek(io, 0) - @test readcsv(io) == [x y] + @test readdlm(io, ',') == [x y] end let x = [0.1 0.3 0.5], io = IOBuffer() @@ -100,13 +100,13 @@ end let x = [0.1 0.3 0.5], io = IOBuffer() writedlm(io, x, ", ") seek(io, 0) - @test readcsv(io) == [0.1 0.3 0.5] + @test readdlm(io, ',') == [0.1 0.3 0.5] end let x = ["abc", "def\"ghi", "jk\nl"], y = [1, ",", "\"quoted\""], io = IOBuffer() writedlm(io, zip(x,y), ',') seek(io, 0) - @test readcsv(io) == [x y] + @test readdlm(io, ',') == [x y] end let x = ["a" "b"; "d" ""], io = IOBuffer() @@ -124,12 +124,12 @@ let x = ["\"hello\"", "world\""], io = IOBuffer() end # test comments -@test isequaldlm(readcsv(IOBuffer("#this is comment\n1,2,3\n#one more comment\n4,5,6")), [1. 2. 3.;4. 5. 6.], Float64) -@test isequaldlm(readcsv(IOBuffer("#this is \n#comment\n1,2,3\n#one more \n#comment\n4,5,6")), [1. 2. 3.;4. 5. 6.], Float64) -@test isequaldlm(readcsv(IOBuffer("1,2,#3\n4,5,6")), [1. 2. "";4. 5. 6.], Any) -@test isequaldlm(readcsv(IOBuffer("1#,2,3\n4,5,6")), [1. "" "";4. 5. 6.], Any) -@test isequaldlm(readcsv(IOBuffer("1,2,\"#3\"\n4,5,6")), [1. 2. "#3";4. 5. 6.], Any) -@test isequaldlm(readcsv(IOBuffer("1,2,3\n #with leading whitespace\n4,5,6")), [1. 2. 3.;" " "" "";4. 5. 6.], Any) +@test isequaldlm(readdlm(IOBuffer("#this is comment\n1,2,3\n#one more comment\n4,5,6"), ','), [1. 2. 3.;4. 5. 6.], Float64) +@test isequaldlm(readdlm(IOBuffer("#this is \n#comment\n1,2,3\n#one more \n#comment\n4,5,6"), ','), [1. 2. 3.;4. 5. 6.], Float64) +@test isequaldlm(readdlm(IOBuffer("1,2,#3\n4,5,6"), ','), [1. 2. "";4. 5. 6.], Any) +@test isequaldlm(readdlm(IOBuffer("1#,2,3\n4,5,6"), ','), [1. "" "";4. 5. 6.], Any) +@test isequaldlm(readdlm(IOBuffer("1,2,\"#3\"\n4,5,6"), ','), [1. 2. "#3";4. 5. 6.], Any) +@test isequaldlm(readdlm(IOBuffer("1,2,3\n #with leading whitespace\n4,5,6"), ','), [1. 2. 3.;" " "" "";4. 5. 6.], Any) # test skipstart let x = ["a" "b" "c"; "d" "e" "f"; "g" "h" "i"; "A" "B" "C"; 1 2 3; 4 5 6; 7 8 9], io = IOBuffer() @@ -213,21 +213,21 @@ let i18n_data = ["Origin (English)", "Name (English)", "Origin (Native)", "Name i18n_arr = permutedims(reshape(i18n_data, 4, Int(floor(length(i18n_data)/4))), [2, 1]) i18n_buff = PipeBuffer() writedlm(i18n_buff, i18n_arr, ',') - @test i18n_arr == readcsv(i18n_buff) + @test i18n_arr == readdlm(i18n_buff, ',') hdr = i18n_arr[1:1, :] data = i18n_arr[2:end, :] writedlm(i18n_buff, i18n_arr, ',') - @test (data, hdr) == readcsv(i18n_buff, header=true) + @test (data, hdr) == readdlm(i18n_buff, ',', header=true) writedlm(i18n_buff, i18n_arr, '\t') @test (data, hdr) == readdlm(i18n_buff, '\t', header=true) end -@test isequaldlm(readcsv(IOBuffer("1,22222222222222222222222222222222222222,0x3,10e6\n2000.1,true,false,-10.34"), Any), +@test isequaldlm(readdlm(IOBuffer("1,22222222222222222222222222222222222222,0x3,10e6\n2000.1,true,false,-10.34"), ',', Any), reshape(Any[1,2000.1,Float64(22222222222222222222222222222222222222),true,0x3,false,10e6,-10.34], 2, 4), Any) -@test isequaldlm(readcsv(IOBuffer("-9223355253176920979,9223355253176920979"), Int64), Int64[-9223355253176920979 9223355253176920979], Int64) +@test isequaldlm(readdlm(IOBuffer("-9223355253176920979,9223355253176920979"), ',', Int64), Int64[-9223355253176920979 9223355253176920979], Int64) # fix #13028 for data in ["A B C", "A B C\n"] @@ -253,13 +253,13 @@ for writefunc in ((io,x) -> show(io, "text/csv", x), let x = [(1,2), (3,4)], io = IOBuffer() writefunc(io, x) seek(io, 0) - @test readcsv(io) == [1 2; 3 4] + @test readdlm(io, ',') == [1 2; 3 4] end # vectors of strings: let x = ["foo", "bar"], io = IOBuffer() writefunc(io, x) seek(io, 0) - @test vec(readcsv(io)) == x + @test vec(readdlm(io, ',')) == x end end @@ -275,12 +275,12 @@ end # issue #21180 let data = "\"721\",\"1438\",\"1439\",\"…\",\"1\"" - @test readcsv(IOBuffer(data)) == Any[721 1438 1439 "…" 1] + @test readdlm(IOBuffer(data), ',') == Any[721 1438 1439 "…" 1] end # issue #21207 let data = "\"1\",\"灣\"\"灣灣灣灣\",\"3\"" - @test readcsv(IOBuffer(data)) == Any[1 "灣\"灣灣灣灣" 3] + @test readdlm(IOBuffer(data), ',') == Any[1 "灣\"灣灣灣灣" 3] end # issue #11484: useful error message for invalid readdlm filepath arguments diff --git a/test/read.jl b/test/read.jl index e3b8cf002887e..ba82a26aaaef6 100644 --- a/test/read.jl +++ b/test/read.jl @@ -261,9 +261,9 @@ for (name, f) in l verbose && println("$name countlines...") @test countlines(io()) == countlines(IOBuffer(text)) - verbose && println("$name readcsv...") - @test readcsv(io()) == readcsv(IOBuffer(text)) - @test readcsv(io()) == readcsv(filename) + verbose && println("$name readdlm...") + @test readdlm(io(), ',') == readdlm(IOBuffer(text), ',') + @test readdlm(io(), ',') == readdlm(filename, ',') cleanup() end