Whenever I want to import data into Netlogo, I usually write a script to convert the file's formatting to be something that is Netlogo friendly before importing it. But so much data comes in CSV (comma separated value) format that it there should be an automatic file-read command for that. There isn't. In lieu of that I have the following two procedures that take care of this. Also note that this includes a Replace-All function for strings in Netlogo, which again it needs but mysteriously lacks.

In this case what I want to do is import some numerical data saved in a CSV file (the median incomes for every district in the US for a 13-year period), and make a big list of all these individual time-series lists. This is done by reading in the file line by line with

file-open "MedianIncomeData.csv"
while [not file-at-end?] [
  let thisline file-read-line

The lines will not read in as you want them; instead you will get a string like this:

"36803  , 38260   , 39702  , 42463, 42183   ..."

I don't know why there are sometimes extra spaces and sometimes not, but just get rid of all the spaces with

set thisline remove " " thisline

Netlogo delineates items in lists with spaces instead of commas, so you need to replace each comma with a space. Unfortunately there is no built in way to do this. There is a String extension that includes this feature, but I couldn't figure out how to get that working (I mean, how to install it). So, I just wrote simple Netlogo subroutine to do this for me:

to-report replace-all [string1 with-string in-string]
  while [ member? string1 in-string ] [
    let index position string1 in-string
    set in-string replace-item index in-string with-string
  ]
  report in-string
end

Be careful, in case you want to use this string or list replace-all function elsewhere that the thing you are replacing with cannot contain the thing being replaced. For example, you can't replace "," with ", " using this because you'll be adding back the comma it replaced and it will run forever (in that case just adding a ton of spaces after the first comma). Anyway, though it's not the perfect general solution to replacement, it's good enough for present purposes.

With that in hand, you can convert the line of CSV, imported as a weird string, into a string formatted as a Netlogo list line with

set thisline (word "[ " (replace-all "," " " thisline) " ]")

Which gives you something like

"[ 36803 38260 39702 42463 42183 ... ]"

Just have Netlogo read that string as a line of input, the list as a real Netlogo list, and attach it to the variable storing your list of lists in Netlogo (in my case MedianIncomeData).

set MedianIncomeData lput read-from-string thisline


All together it looks like this:

to import-data
  set MedianIncomeData []
  file-open "MedianIncomeData.csv"
  while [not file-at-end?] [
    let thisline file-read-line
    ;show thisline
    set thisline remove " " thisline
    set thisline (word "[ " (replace-all "," " " thisline) " ]")
    ;show thisline
    set MedianIncomeData lput read-from-string thisline MedianIncomeData
  ]
  file-close
end


You'll need to do more tweaking if you want to combine strings and numbers, but the basic building blocks are there. This will make it faster and easier to import CSV data into Netlogo instead of using Excel or Perl scripts to rewrite your data as a txt file that is already Netlogo friendly for input. Though currently I'm using this for ordinary time-series data, it is also clearly useful for other data coming out of other programs; such as adjacency matrices, Markov matrices, agent attributes, etc.