How to save multiple output lines to a txt or csv file?

Similar to How do I save the output to .txt file?, I would like to know how to save several output lines into a file.

Currently my function runs a for loop, and if certain criteria are met it println’s certain values into the REPL. I leave it running over night and manually copy the results into an excel sheet. This is obviously not secure and I would like to know how to save it directly into a file.

The last part of the function looks like this, which I had hoped would work.

        if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io #here I think "a" refers to append! because I am adding more to the file 
# This is not clear in the previously linked example because the person also called what they wanted to print as a
# "Fish parameters 80 species.txt" has been previously created
                println(io,"target_species: ", target_species)
                println(io,"catch_max: ", catch_max2)
                println(io,"scaling: ", scaling2)
                println(io,"maintenance: ", maintenance2)
                println(io,"P_catch: ", P_catch2)
                println(io,"μ: ", μ2)
            end
        end

So far nothing has been written in the file and I would like to know why.

1 Like

This looks correct, but I guess you need to call flush(io) or close(io) at the end, so that the buffer is written out into your file.

Like so?

if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io
                 println(io,"target_species: ", target_species)
                 println(io,"catch_max: ",catch_max2)
                 println(io,"scaling: ",scaling2)
                 println(io,"maintenance: ",maintenance2)
                 println(io,"P_catch: ",P_catch2)
                 println(io,"μ: ",μ2)
            end
           close(io)
        end

Or so? (Sorry, it takes a bit to run the function and with the if statement I am not sure if it can’t print or if the if statement wasn’t fulfilled)

if (sol3[(num_nutrient+num_plant+num_animal+num_vessel_types),end]) > 0.000001 && (sol3[target_species,end] > 0.000001)
            open("Fish parameters 80 species.txt","a") do io
                 println(io,"target_species: ", target_species)
                 println(io,"catch_max: ",catch_max2)
                 println(io,"scaling: ",scaling2)
                 println(io,"maintenance: ",maintenance2)
                 println(io,"P_catch: ",P_catch2)
                 println(io,"μ: ",μ2)
            close(io)
            end
        end

This is a very beginner question, but does the file that you want to write in need to be in your working directory?

Edit: Julia will magically make a document for you in the working directory

It writes when I use the format of

open("Fish parameters 80 species.csv","a") do io
      println(io,"target_species: ", target_species)
      println(io,"catch_max: ",catch_max2)
      println(io,"scaling: ",scaling2)
      println(io,"maintenance: ",maintenance2)
      println(io,"P_catch: ",P_catch2)
      println(io,"μ: ",μ2)
      close(io)
end

Whether this is actually the correct format I don’t know. But it works.

It shouldn’t - when you use the do block like this, the buffer is closed at the end of the loop.

No, but you need to provide the correct path (can be absolute or relative)

That looks fine, though if you’re making a CSV I highly recommend putting stuff in a team or format then using CSV.jl

2 Likes

Thank you for your reply. Could you explain what you mean by team or format?

For CSV files there is a package which you might find useful Home · CSV.jl

Heh… phone typo. Should have been “Put stuff in a Tables.jl format then…”