readcsv
readcsv(source, [T::Type]; options...)
Equivalent to readdlm
with delim
set to comma.
Examples
In the Julia programming language, the function readcsv(source, [T::Type]; options...)
is equivalent to readdlm
with delim
set to comma. It reads data from a CSV file or any source that uses comma as the delimiter and returns it as a matrix or a DataFrame.
julia> using CSV
julia> data = readcsv("data.csv")
3×2 Matrix{Any}:
1 "John"
2 "Jane"
3 "Alice"
julia> typeof(data)
Matrix{Any}
julia> df = CSV.read("data.csv")
3×2 DataFrame
Row │ Column1 Column2
│ Int64 String
─────┼─────────────────
1 │ 1 John
2 │ 2 Jane
3 │ 3 Alice
Here are some common examples of how to use readcsv
:
-
Read a CSV file into a matrix:
julia> data = readcsv("data.csv")
-
Read a CSV file into a DataFrame using the CSV.jl package:
julia> using CSV julia> df = CSV.read("data.csv")
-
Specify the type of the columns when reading into a DataFrame:
julia> using CSV julia> df = CSV.read("data.csv", types=[Int64, String])
Common mistake example:
julia> data = readcsv("data.csv", Int)
ERROR: MethodError: no method matching readcsv(::String, ::Type{Int64})
In this example, the Int
argument is passed as a type parameter to readcsv
, which is not supported. To specify the type of the columns, you need to use the types
keyword argument as shown in the third example above.
See Also
abspath, basename, chmod, countlines, cp, ctime, dirname, download, evalfile, expanduser, fdio, filemode, filesize, functionloc, gperm, homedir, include_string, isabspath, isblockdev, ischardev, isdir, isdirpath, isexecutable, isfifo, isfile, islink, ismount, ispath, isreadable, issetgid, issetuid, issticky, iswritable, joinpath, less, lstat, mkdir, mkpath, mktemp, mktempdir, mtime, mv, normpath, operm, poll_fd, poll_file, readall, readcsv, readdir, readdlm, readlines, readlink, realpath, relpath, rm, splitdir, splitdrive, splitext, stat, symlink, tempdir, tempname, touch, truncate, uperm, watch_file, writecsv,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.