CSV Document not reading properly

Viewed 61

Im trying to add variables to my turtles based on an Excel document. The excel document is just two columns with 19 random numbers. It is saved as a CSV. Im getting an error that says : Expected a literal value. (line number 19, character 2) error while turtle 17 running FILE-READ

However, all values are numbers in the document. This is the code

extensions [csv] 
globals [ turtle-data ]
 turtles-own [ awareness income housingtype adopt ]
 to setup
  clear-all
  reset-ticks
  create-turtles 20
  ask turtles [
   set color white
  ]
  ask turtles [
   setxy random-xcor random-ycor
  ]
; load-turtle-data
   ; setup-turtles
  load-data
 end

to load-data
  file-open "Ejemplocsv.csv"
  while [not file-at-end?] [
   ask turtles [
      set income file-read
      set housingtype file-read
      set size 1.5]
    ask turtles [setxy random-xcor random-ycor]]
  
  file-close
  show "file read"

end

I also tried instead a code that another user posted here but got the same error. This part replaces the load-data. It doesn't work either.


to load-turtle-data
 ifelse ( file-exists? "Ejemplocsv.csv" ) [
   set turtle-data []
   file-open "Ejemplocsv.csv"
   while [ not file-at-end? ][
      set turtle-data sentence turtle-data (list (list file-read file-read))
   ]
  user-message "File loading complete!"
  file-close
  ]
  [
     user-message "There is no input-data.txt file in current directory!" 
  ]
end

to setup-turtles
 foreach turtle-data [ iter ->
   crt 1 [
     set income item 0 iter
     set housingtype item 1 iter
   ]
 ]
end

I just want to know the most effective way to load csv data, or excel, to the variable of my turtles.

1 Answers

The problem is likely that your csv file is indeed comma delimited, not space delimited. file-read expects constants to be separated by white space and commas do not count as white space. If you saved your Excel file with spaces as the delimiter, or if you used a text editor to change the commas to spaces in your csv file, your code should work.
But, if you are going to load the csv extension, you might as well use it. The following code uses one command to read the whole csv file into turtle-data, as a list of lists, and setup-turtles then creates the turtles and uses turtle-data to populate their income and housingtype values. csv:from-file is perfectly happy with commas. (Note you don't need file-open or file-close with csv:from-file as it does the opening and closing for you.)

extensions [csv]
globals [ turtle-data ]
turtles-own [ awareness income housingtype adopt ]

to load-turtle-data
  ifelse ( file-exists? "Ejemplocsv.csv" ) [
;    file-open "Ejemplocsv.csv"
    set turtle-data csv:from-file "Ejemplocsv.csv"
    user-message "File loading complete!"
;    file-close
  ]
  [
    user-message "There is no input-data.txt file in current directory!"
  ]
end

to setup-turtles
  foreach turtle-data [ iter ->
    crt 1 [
      set income item 0 iter
      set housingtype item 1 iter
    ]
  ]
end
Related