CSV and excel.

edited August 2016 in How To...

Hi! I'm making a sketch where I use excel to create a csv, but when I save it as a csv from excel it seperates the cells with ';' instead of a comma. That's actually a great idea, because then you can have sentences with comma in the cells. But processings loadTable() function uses the comma to separate instead of the semi-colon. Is it possible to change this? Can I make processing use the semi-colon instead?

Answers

  • I found the method in the PApplet, but I'm not sure how to use it...:

     public Table loadTable(String filename, String options) {
        try {
          String optionStr = Table.extensionOptions(true, filename, options);
          String[] optionList = trim(split(optionStr, ','));
    
          Table dictionary = null;
          for (String opt : optionList) {
            if (opt.startsWith("dictionary=")) {
              dictionary = loadTable(opt.substring(opt.indexOf('=') + 1), "tsv");
              return dictionary.typedParse(createInput(filename), optionStr);
            }
          }
          InputStream input = createInput(filename);
          if (input == null) {
            System.err.println(filename + " does not exist or could not be read");
            return null;
          }
          return new Table(input, optionStr);
    
        } catch (IOException e) {
          printStackTrace(e);
          return null;
        }
      }
    
  • I tried your solution @GoToLoop, but It's a bit messy. I want a table because it has cells, so I tried to do this:

    String lines[] = loadStrings("bok.txt");
      String[][] table;
    
    //in setup()
    for (int i = 0; i < lines.length; i++) {
          String[] rows = split(lines[i], ';');
          for (int j = 0; j< rows.length; j++) {
            table[i][j] = rows[j];
          }
        }
    

    Basically just making a 2d array of the strings. But I'm getting a NullPointerException highlighting line 8.

  • edited August 2016

    You haven't even initialized your 2D array; merely just declared the variable.
    Regardless, that's not a good move. You're better off creating some class in order to represent each row that was read from your file: https://Processing.org/examples/loadfile2.html

Sign In or Register to comment.