Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when reading CSV files. #832

Closed
FedX-sudo opened this issue May 2, 2021 · 7 comments
Closed

Error when reading CSV files. #832

FedX-sudo opened this issue May 2, 2021 · 7 comments

Comments

@FedX-sudo
Copy link

Whenever I read a CSV file via CSV.read("data.csv") I run into the error

ERROR: LoadError: UndefVarError: CSV not defined

I ran the Pkg test CSV command and this is the result:

Test Summary:                  | Pass  Fail  Total
     CSV                       | 1830     1   1831
     CSV.File                  | 1628         1628
     CSV.write                 |   92     1     93
     PooledArrays              |   14           14
     LazyStringVector          |    7            7
     CSV.Rows                  |   37           37
     CSV.detect                |    8            8
     CSV.findrowstarts!        |    4            4
     CSV.promote_typecode      |    2            2
     CSV.File with select/drop |   38           38

ERROR: LoadError: Some tests did not pass: 1830 passed, 1 failed, 0 errored, 0 broken.
in expression starting at /home/fedx/.julia/packages/CSV/CJfFO/test/runtests.jl:13
ERROR: Package CSV errored during testing
@quinnj
Copy link
Member

quinnj commented May 3, 2021

You have to make sure you do using CSV, DataFrames before you call CSV.read("data.csv", DataFrame).

@FedX-sudo
Copy link
Author

I did make sure I was using DataFrames however, I did not realize that CSV reading required the DataFrame part, is that a predefined variable?

@quinnj
Copy link
Member

quinnj commented May 3, 2021

CSV doesn't require DataFrames.jl. When call CSV.read(filename, sink), however, you're required to provide a 2nd argument of the kind of thing you want to read the csv file into. Alternatively, you can just call f = CSV.File(filename) and you get a CSV.File object returned, which can be useful for most people. You can iterate rows like:

for row in CSV.File(file)
    # so something with row.col1, for example
end

Or access entire columns like:

col1 = f.col1

@FedX-sudo
Copy link
Author

FedX-sudo commented May 3, 2021

Ok, I am sending the output into an empty data frame, via

df = DataFrame()
CSV.read("data.csv", df)

however, I am still reciving the "csv not defined" error.

@quinnj
Copy link
Member

quinnj commented May 3, 2021

You need to load the CSV.jl package first. When you start the julia program, the CSV.jl package isn't loaded yet, so you need to do something like:

using CSV, DataFrames
df = CSV.read("data.csv", DataFrame)

Note that for the 2nd argument to CSV.read, you pass DataFrame, which is a function that tells the CSV package how to read the csv file into.

@FedX-sudo
Copy link
Author

Ok, I did load the package in the last program, but the issue was I needed to define the df variable using CSV.read. Sorry for taking up your time and thank you so much for your help!

@quinnj
Copy link
Member

quinnj commented May 3, 2021

No problem; happy to help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants