How to read a .CSV file with netlogo?

為{幸葍}努か 提交于 2019-12-01 17:49:14

问题


I'm starting in netlogo these days, so I've got some problems that I didn't find how to solve them. I have to read a huge .csv file, got this code on the web:

to openFile
file-open "testeCsv.csv"
set csv file-read-line
set csv word csv ","  ; add comma for loop termination 

let mylist []  ; list of values 
  while [not empty? csv] 
  [
    let $x position "," csv 
    let $item substring csv 0 $x  ; extract item 
    carefully [set $item read-from-string $item][] ; convert if number 
    set mylist lput $item mylist  ; append to list 
    set csv substring csv ($x + 1) length csv  ; remove item and comma 
    set fileList mylist
  ] 
  set fileList mylist
  show fileList
end

This file contains this line: "1;0;0;65;0;2;45;0;-0,018961934" The output of this code is: "1 18961934" Help :/


回答1:


The code seems basically correct. I assume that csv and fileList are defined as global variables, otherwise the "set" commands on each will generate compile-time errors. We can also delete the reference to fileList in the while block since it is reset anyway once the while loop exits. Then, if you want to keep the semicolon as the separator we can make that change as well. Altogether, we get:

globals [csv fileList]

to openFile
file-open "testeCsv.csv"
set csv file-read-line
set csv word csv ";"  ; add semicolon for loop termination 

let mylist []  ; list of values 
  while [not empty? csv] 
  [
    let $x position ";" csv 
    let $item substring csv 0 $x  ; extract item 
    carefully [set $item read-from-string $item][] ; convert if number 
    set mylist lput $item mylist  ; append to list 
    set csv substring csv ($x + 1) length csv  ; remove item and comma 
  ] 
  set fileList mylist
  show fileList
end

When I run this on the line in the CSV file you provide (after converting the decimal separator in the file from "," to ".") I get the result you seek, [1 0 0 65 0 2 45 0 -0.018961934].

Of course that reads only one line in the file. I assume that you will want to loop through each line in the file, storing or using each line as it is read. The following stores each line in a list of lists.

globals[csv fileList]

to openFile
  file-open "testeCsv.csv"
  set fileList []

  while [not file-at-end?] [
    set csv file-read-line
    set csv word csv ";"  ; add comma for loop termination 

    let mylist []  ; list of values 
    while [not empty? csv] 
    [
      let $x position ";" csv 
      let $item substring csv 0 $x  ; extract item 
      carefully [set $item read-from-string $item][] ; convert if number 
      set mylist lput $item mylist  ; append to list 
      set csv substring csv ($x + 1) length csv  ; remove item and comma 
    ] 
    set fileList lput mylist fileList
  ]

  show fileList
  file-close
end

Hope this helps. Charles




回答2:


First of all, CSV means comma separated values with a point decimal separator, not semi-colon separated values with a comma decimal separator. See RFC 4180. Although some have resisted this for nationalistic reasons, the scientific community needs to adopt the existing standard. Agent-based modeling is part of the scientific community.

Now let's analyze what happen when you parse your file with this code. It reads the line as a string into your csv variable. It sets $x to 20 and extract the substring up to the comma. The use of read-from-string treats "1;0;0;65;0;2;45;0;-0" as if it had been typed at the command center, so you just get the number 1, since everything from the first semicolon onwards is a comment. The 1 is put into mylist and the csv variable is shortened to 018961934. When this is read, you get 18961934 because NetLogo ignores the leading 0.

This all happens because you do not have a real CSV file. So you need to use a real CSV file if you want to use this code to parse it.



来源:https://stackoverflow.com/questions/27096948/how-to-read-a-csv-file-with-netlogo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!