How to read a .CSV file with netlogo?

前端 未结 2 1142
故里飘歌
故里飘歌 2021-01-20 01:40

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:

相关标签:
2条回答
  • 2021-01-20 02:03

    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
    
    0 讨论(0)
  • 2021-01-20 02:03

    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.

    0 讨论(0)
提交回复
热议问题