csv file and convert

edited November 2015 in How To...

i had a csv file, this file stores thousands numbers in it . it looks like this when i use the println to print it out ! Screen Shot 2015-11-19 at 1.04.02 PM my question is how to convert this string to an int for every number in this file? example : - the 1st number will be x - the 2nd will be y - the 3rd will be color - the 4th will be another x - the 5th will be another y - the 6th will be another color the point(x,y) and color together will make an image but i need to convert them to int first before i can do other step

i had looked at the way to do it, but i did not get any result Thanks so much if you can have look and help me oh i cannot use array for this question

Answers

  • i did read all of this, but they did not work, since the string is not there like String s = " 12,14,15,13,13 " Do you get what i meant ?

  • Indeed Processing's reference is too incompetent to even have some CSV example in loadString() reference!

    Go to "Reading and Writing Text Files" section @ https://Processing.org/tutorials/data/

  • In the original csv file the 3 values are just separated by comma and eaxh 3 values by line break?

  • the file contains thousands of values and each of them separate by the comma

  • like this

    324,31,213
    213,211,31
    

    ?

    is it so?

  • Answer ✓

    that would apply when you have lines with 3 values on each line (like shown above)

    324,31,213
    213,211,31
    

    does only work with lines of this format

    int[][] data = new int [1][1];
    
    void setup() {
      size(1200, 900);
    
      // Load text file as a string
      String[] stuff = loadStrings("data.txt");
    
      data = new int [stuff.length][3];
    
      // Convert string into an array of integers using ',' as a delimiter
      for (int i=0; i<stuff.length; i++) {
        data[i] = int(split(stuff[i], ','));
      }
    }// func 
    
    void draw() {
      background(255);
      // stroke(0);
      noStroke(); 
      for (int i = 0; i < data.length; i++) {
        fill(data[i][2]);
        rect(data[i][0], data[i][1], 6, 6);
      }
    }
    //
    
  • Answer ✓
    // this version assumes only one huge line / paragraph in the file.
    //
    // It is stored in stuff[0] then.
    //
    // It has clusters of three numbers seen as one point (x,y,color). Therefore the for-loop
    // basically jumps from start of one triple of numbers to the next (i=i+3).
    
    int[] data;
    
    void setup() {
      size(1200, 900);
    
      // Load text file as a string
      String[] stuff = loadStrings("data.txt");
    
      println(stuff.length); 
      println("******************************"); 
    
      data = new int [stuff.length];
    
      // Convert string into an array of integers using ',' as a delimiter
      data = int(split(stuff[0], ','));
    }// func 
    
    void draw() {
      background(255);
    
      for (int i = 0; i < data.length-3; i=i+3) {
        // do the rect
        fill(data[i+2]);
        noStroke(); 
        rect(data[i], data[i+1], 6, 6);
        // additional text 
        if (!keyPressed) {
          fill(0); 
          text(i
            +":"
            +data[i] 
            +","
            + data[i+1] 
            + "|"
            + data[i+2], 
            data[i]+9, data[i+1]+5);
        } // if
      } // for
    }
    //
    
  • i am so sorry when this has to work with only string, without array

  • You convert the string to array

  • Answer ✓

    You can also read the string char by char with charAt and on the way add the values until you reach a comma,

    again wait till you have 3 values and then produce the point/ rect

    Very inconvenient

  • yeb , it is true, it is not convinience

  • Show your attempt

Sign In or Register to comment.