readcsv(source, [T::Type]; options...)

Equivalent to readdlm with delim set to comma.


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)

julia> df ="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:

  1. Read a CSV file into a matrix:

    julia> data = readcsv("data.csv")
  2. Read a CSV file into a DataFrame using the CSV.jl package:

    julia> using CSV
    julia> df ="data.csv")
  3. Specify the type of the columns when reading into a DataFrame:

    julia> using CSV
    julia> df ="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.

*Required Field

Checking you are not a robot: