How to use csv: to-file?

61 views
Skip to first unread message

Rafaela Lorena

unread,
Feb 25, 2022, 10:56:09 AM2/25/22
to netlogo-users

Hi all,

I have a question about using the csv extension in NetLogo. I would like to export a .csv file using the csv extension. However, my output is per tick and when using csv: to-file it only writes the last tick. It's like csv:to-file would delete previous ticks. I looked for ways not to delete and couldn't find a solution. Does anyone know if it is possible to use csv: to-file per tick, without erasing previous ticks? I need to use csv because I am working with lists. OBS.: It is not possible to accumulate the values generated at each iteration, as the list would be large and ended up having a memory problem

Thanks in advance

Below is a code showing that csv: to-file deletes the previous information (only the values 5.6 and 7.8 appear in the file

    to test
    csv:to-file (" test-1.csv" ) [ [1 2] [ 3 4 ] ]
    csv:to-file (" test-1.csv" ) [ [5 6] [ 7 8 ] ]
    end

Charles Staelin

unread,
Feb 25, 2022, 12:12:26 PM2/25/22
to netlogo-users
I think that what you need to do here is to use "csv:to-string" along with "file-print"  E.g., the following code will give you what you want.  Just setup and repeat test several times to test it.

extensions [csv]
to setup
  clear-all
  file-close-all
  file-open "test-1.csv"
  reset-ticks
end
 
to test
  let out-list [ [1 2] [ 3 4 ] ]
  set out-list lput (list ticks) out-list
  let out-string csv:to-string  out-list
  file-print out-string
  tick
end

The documentation for csv:to-file says that it creates a new file.  I too didn't realize it did that even if the file already exists.

Charles

Reply all
Reply to author
Forward
0 new messages