Lancelac Dulot
Lancelac Dulot

Reputation: 27

How create output csv with julia?

I'm new user of Julia. I try to create one code for an start up.

begin
    using Pkg
    Pkg.add("CSV") 
    Pkg.add("DataFrames")  
    using Statistics
    using DataFrames
    using CSV
    using Dates
    v=[1:12]
    resultat=CSV.File("ResultatPropre.csv";header=true; delim=";")
    println(resultat)
    i=1
    while i <= 12
        a=now()     ##Dates.millisecond
        dt = CSV.read(resultat[i,1]*"_"*resultat[i,2]*"_"*resultat[i,3]*"_"*resultat[i,4]*"_1.csv")
        x=resultat[i,6]
        moy="MOYENNE"
        if x==moy
            for c in eachcol(dt)
                println(mean(dt[:c]))
            end 
        else
            for c in eachcol(dt)
                println(median(dt[:c]))
            end
        end 
        v[i]=now()-a
        close(dt)
        i = i + 1 
    end
    CSV.write("OUT1.csv",  DataFrame(hcat(resultat,v)), writeheader=false)
    close(resultat)
end

I don't know if this code is correct but i haven't error message. The document OUT1.CSV is empty. Why?

Sorry if you can't understand I'm not fluent in english. Thank you.

Upvotes: 2

Views: 329

Answers (1)

Nils Gudat
Nils Gudat

Reputation: 13800

As with your previous question this is hard to debug, as it relies on files that are stored on your local machine - it is easiest for others to help you if you can create minimal working examples that reproduce the error you're getting.

From what you posted, there is an obvious issue though with what you're trying to do:

resultat=CSV.File("ResultatPropre.csv";header=true; delim=";")

This will return a CSV.File object, and not a DataFrame as you might expect. Consider the following example:

julia> using DataFrames, CSV

julia> CSV.write("out.csv", DataFrame(rand(5, 2))) # example data
"out.csv"

julia> resultat = CSV.File("out.csv")
5-element CSV.File{false}:
 CSV.Row{false}: (x1 = 0.8579220366916582, x2 = 0.6209363986752581)
 CSV.Row{false}: (x1 = 0.25341118271903995, x2 = 0.13828085618933872)
 CSV.Row{false}: (x1 = 0.67532944746357, x2 = 0.7830459406731047)
 CSV.Row{false}: (x1 = 0.268297279369758, x2 = 0.9701649420771219)
 CSV.Row{false}: (x1 = 0.8369770803698637, x2 = 0.77439272213442)

This is probably not what you expected, given that you hcat resultat and your v vector later on.

The line that will actually error in your code however is:

close(dt)

At this point, dt is a DataFrame, as you've created it by calling CSV.read on a csv file, and you are calling the close function on this DataFrame. However, a close method does not exist for DataFrames:

julia> close(DataFrame(rand(5,2)))
ERROR: MethodError: no method matching close(::DataFrame)

The result of CSV.read is a DataFrame that is stored in memory, and there is no "open" file handle anywhere that needs to be closed after performing operations on your DataFrame - CSV.read is different from calling open on some text file that you then iterate through.

Upvotes: 1

Related Questions