Reading from .CSV file


I've been working on a project that involves rows of household community data. The records are stored in an Excel file which I converted to a CSV file. This file contains a number of information that must be assigned as attributes of the household agents. Each line represents one agent record with each value separated using a comma. Here's how the data looks like:

1,1,1,1,4,2,2,2,1,3
2,1,2,2,4,2,1,5,1,2
3,1,1,1,1,2,2,7,2,3
4,1,1,1,4,2,1,4,1,1
5,1,1,2,3,4,4,6,1,2
6,1,1,2,4,2,2,3,1,3
7,1,3,2,3,1,3,9,1,4
8,1,2,2,4,4,1,3,1,2
9,1,2,1,4,1,2,5,1,2
10,1,1,1,4,1,5,8,1,2

Below is the openFile procedure which uses two variables, csv to handle the record retrieved per line and fileList to hold all the formatted records (comma separators are removed).  



Sample Output:

[[1 1 1 1 4 2 2 2 1 3] [2 1 2 2 4 2 1 5 1 2] [3 1 1 1 1 2 2 7 2 3] [4 1 1 1 4 2 1 4 1 1] [5 1 1 2 3 4 4 6 1 2] [6 1 1 2 4 2 2 3 1 3] [7 1 3 2 3 1 3 9 1 4] [8 1 2 2 4 4 1 3 1 2] [9 1 2 1 4 1 2 5 1 2] [10 1 1 1 4 1 5 8 1 2]] 
Share on Google Plus

About SSabal

:)
    Blogger Comment
    Facebook Comment

5 comments:

  1. I made a button for this code. For a four-row table I have to click four times and fileList includes one row each time. Is there a way for fileList to include the whole table in one click and also show the whole table in one click? It seems in each click it read one line.

    ReplyDelete
    Replies
    1. Sorry for this late feedback. I am not sure if I get your question right but this code will fetch all the rows from an external file. Did you tick your button configuration to "forever" so that it's going to be executed repeatedly?

      Delete
    2. I agree with Elham. Yes the code will import all the row but row by row on each tick (whether it is manual or forever). Elham asked how to show all import data in one click? I also want to ask how to solve errors after the last row imported. The error message:

      The end of file has been reached
      error while observer running FILE-READ-LINE
      called by procedure OPENFILE
      called by Button 'Open'

      Delete
  2. Hi.

    What does the $ symbol means? Thanks.

    ReplyDelete
  3. Hi, I understand the code, but I would like save that list in one variable and then assign each value for each turtle.
    Someone could help me? please

    ReplyDelete