How to use the CSV.read? error "provide a valid sink argument"

Hey everyone, I’m trying to use the CSV.read method, however I keep getting the argument “provide a valid sink argument”. I’ve tried putting in the path to my csv file I’m trying to load, but it still doesn’t work. The code I’ve used is as follows:

‘’’
Pkg.add(“CSV”)
Pkg.add(“DataFrames”)
using CSV, DataFrames

project_data = using DataFrames; CSV.read(“/Users/odionhillocks/Desktop/Project6925/Data.csv”, DataFrame)

‘’’

Hi Marc, welcome to the community! It’s hard to diagnose this sort of issue if the reader can’t reproduce it. Can you provide more details (ideally a reproducible example following these guidelines)?

Try using CSV.File instead of CSV.read; I believe the latter might be only for reading into a specific Tables.jl-compatible sink (which is not what most users actually want).

you want the following

using DataFrames, CSV
df = CSV.read(file, DataFrame)

But what goes in the place of “file” I tried putting the path to my file and it doesn’t work, if i put the file name only, it tells me the file is not valid.

The code I tried is placed below:

Pkg.add(“CSV”)
Pkg.add(“DataFrames”)
using CSV, DataFrames

project_data = using DataFrames; CSV.File(“/Users/odionhillocks/Desktop/Project6925/Data.csv”, DataFrame)

examples from documentation:

df = CSV.File(file) |> DataFrame

file is the path to your file (basically what you already have up there)

1 Like

your code has CSV.File(file, DataFrame). You need CSV.read(file, DataFrame)

This is what I have now and I’m still getting the “provide a valid sink” error.

Pkg.add(“CSV”)
Pkg.add(“DataFrames”)
using DataFrames, CSV

project_data = using DataFrames; CSV.read(“/Users/odionhillocks/Desktop/Project6925/Data.csv”, DataFrame)

Can you run the following code?

julia> using CSV, DataFrames

julia> df = DataFrame(a = [1, 2], b = ["x", "z"]);

julia> CSV.write("test.csv", df);

julia> df2 = CSV.read("test.csv", DataFrame)

Thanks! This code runs, then I retried my old code right after and it works now. Thanks so much for your assistance!

Late to the party, but this line

project_data = using DataFrames; CSV.read("/Users/odionhillocks/Desktop/Project6925/Data.csv", DataFrame)

was split in two, due to the ; character

project_data = using DataFrames
CSV.read("/Users/odionhillocks/Desktop/Project6925/Data.csv", DataFrame)

So, you read output of using DataFrames in project_data variable (which is a weird thing, suppose it was nothing?) and after that julia read csv file and ignore it.

If you need to use ; symbol, you should put it into brackets

project_data = (using DataFrames; CSV.read("/Users/odionhillocks/Desktop/Project6925/Data.csv", DataFrame))

but there is no sense in doing it in this line of code. Also do not put using statements inside calculations, it’s better to import library at the beginning of the script.