Problem with reading a .csv file

Hello, I have a problem in my program when I decide to read a .csv file, it appears line by line but each word is separated by a semicolon (I have trouble explaining this problem but I think that you understand what is happening to me). In fact, the purpose of my program is to make a table with 3 columns in processing but suddenly I wonder how to avoid semicolons.
Thank you for taking the time to read this.

If the semicolon ; character is the delimiter for separating the values within a row, it’s not a valid CSV :registered: file anymore!

Instead, you’re gonna need to rely on the following trio: :hammer_and_pick:

  1. loadStrings(): https://Processing.org/reference/loadStrings_.html
  2. split(): https://Processing.org/reference/split_.html
  3. trim() (rarely needed though): https://Processing.org/reference/trim_.html

Actually, you could be more specific. Under what conditions are you seeing these semicolons? Are they:

  1. In the original data file if you open it in a plain text editor
  2. appearing in a printout in the console if you use println() – if so, please share with what code exactly are you displaying,
  3. is appearing on the screen of your sketch? If so, it sounds like you might have an extra semicolon character in your input to text(), or in your use of the join() statement.

Please share your code!

Hello, thank you for answering me. I will show you the problem in question with screenshots because yesterday I was not very explicit. The first screenshot shows what my problem is and the second is what I would like to get by removing the semicolons.
https://imgur.com/JdwjmeS
https://imgur.com/e3VP8x8

Thank you for showing how they appear on your screen. Now, please share your code!

There is no way to know wha the problem is unless you answer some of the questions like “are they in the original data file” or "do you have an extra semicolon character in your join() statement?

Hello and sorry for not being able to answer earlier. So little update, a friend helped me a little, I fixed the problem of the semicolon (which was in this line of code: String [] lines2 = split (lines [M], ‘;’) ) and managed to draw a picture. But by doing this the array no longer reads the values of the .csv but values encoded in processing and is placed at the top left without me finding how to move it. I share the code below.

int M=1;
PFont police;
PImage logo;
String[] name = {"Jones", "McHale", "Jacks"};
String[] subject      = {"Math", "English", "Spanish"};
String[] schedule     = {"M4", "S2", "S4" };

void setup() {
  size(displayWidth, displayHeight);
  logo = loadImage("logojules_LI.jpg");
  rectMode(CENTER);
  textAlign(CENTER);
}

void draw() {
  if (M==1) {
    home();
    board();
  }
}

void home() {
  M=1;
  police = loadFont("BaskOldFace-49.vlw");
  textFont(police, 49);
  background(255, 255, 255);
  fill(70, 130, 180);
  if (mouseX > displayWidth/25 && mouseX < displayWidth/3.42 && mouseY > displayHeight/2.8 && mouseY < displayHeight/2.26 && M==1) {
    cursor(HAND);
  } else
      if (mouseX > displayWidth/25 && mouseX < displayWidth/3.42 && mouseY > displayHeight/1.49 && mouseY < displayHeight/1.32 && M==1) {
        cursor(HAND);
      } else {
        cursor(ARROW);
      }

  rect(displayWidth/2, displayHeight/12, displayWidth*2/5, displayHeight/10);
  rect(displayWidth/1.5, displayHeight/4, displayWidth/2, displayHeight/10);
  rect(displayWidth/6, displayHeight/2.5, displayWidth/4, displayHeight/12);
  //rect(displayWidth/6, displayHeight/1.8, displayWidth/4, displayHeight/12);
  rect(displayWidth/6, displayHeight/1.4, displayWidth/4, displayHeight/12);

  fill(0, 0, 0);
  text("Professors absent", displayWidth/2, displayHeight/10.5);
  text("Today", displayWidth/1.5, displayHeight/3.8);
  text("Next day", displayWidth/6, displayHeight/2.4);
  text("Contact information", displayWidth/6, displayHeight/1.37);

  imageMode(CENTER);
  image(logo, displayWidth/6, displayHeight/5, displayWidth/5, displayHeight/5);
}

  void board() {
   textFont(police, 30); 
 for (int line = 0; line < name.length; line ++) {
    for (int column = 0; column < 3; column++) {
      int x = column*200;
      int y = line*50;
            
      fill(255);
      stroke(0);
      strokeWeight(2);
      
      rect(x,y,displayWidth/4, displayHeight/12);
      
      String text_affiche = "";
      
        if (column == 0) {
          text_affiche = name[line];
        }
        if (column == 1) {
          text_affiche = subject[line];
        }
        if (column == 2) {
          text_affiche = schedule[line];
        }
        
        fill(0);
        noStroke();
        text(text_affiche,x+20,y+30);
        
      }
    }
  }

void contact() {
  M=2;
  background(255, 255, 255);
  fill(70, 130, 180);

if (mouseX > displayWidth*4/10 && mouseX < displayWidth*6/10 && mouseY > displayHeight*20/24 && mouseY < displayHeight*22/24 && M==2) {
  cursor(HAND);
} else {
  cursor(ARROW);
}

rect(displayWidth/2, displayHeight/12, displayWidth*3/5, displayHeight/12);
rect(displayWidth/2, displayHeight*4/16, displayWidth/2, displayHeight/12);
rect(displayWidth/2, displayHeight*6/16, displayWidth/2, displayHeight/12);
rect(displayWidth/2, displayHeight*8/16, displayWidth/2, displayHeight/12);
rect(displayWidth/2, displayHeight*10/16, displayWidth/2, displayHeight/12);
rect(displayWidth/2, displayHeight*12/16, displayWidth/2, displayHeight/12);
rect(displayWidth/2, displayHeight*14/16, displayWidth/5, displayHeight/12);

fill(0, 0, 0);
text("Coordonnées du Lycée", displayWidth/2, displayHeight/10);
textFont(police, 30);
text("Lycée Marcellin Berthelot", displayWidth/2, displayHeight*26/100);
text("Avenue Roland Garros", displayWidth/2, displayHeight*36.5/100);
text("56 231 QUESTEMBERT", displayWidth/2, displayHeight*40.5/100);
text("02 97 26 12 06", displayWidth/2, displayHeight*51/100);
text("Ce.0561641e@ac-rennes.fr", displayWidth/2, displayHeight*61.5/100);
text("vie-scolaire1.0561641e@ac-rennes.fr", displayWidth/2, displayHeight*65.5/100);
text("MENU", displayWidth/2, displayHeight*88.5/100);
textFont(police, 20);
text("s.agneessens", displayWidth/2, displayHeight*79/100);
text("julesguimard9", displayWidth/2, displayHeight*76/100);
text("julien.lamirault46", displayWidth/2, displayHeight*73/100);
}

void mousePressed() {
  if (mouseX > displayWidth/25 & mouseX < displayWidth/3.42 & mouseY > displayHeight/1.49 & mouseY < displayHeight/1.32 & M==1) {
    contact();
  }
  if (mouseX > displayWidth*4/10 & mouseX < displayWidth*6/10 & mouseY > displayHeight*20/24 & mouseY < displayHeight*22/24 & M==2) {
    home();
  }
}
1 Like

Congratulations on your progress!

So your questions are:

  1. how do I move ? from the top left to …?
  2. how do I load ? from a CSV file?

I’m trying to guess here, but could you be a lot more specific? Give details of your program; this helps people help you.

Hello, I will try to be a little more specific. So for the table, I do not see how to move it where I would like to show on the screenshot (I do not know which line of the code handles that). And for the csv, currently the table reads data that I code myself in processing. What I want is to read a csv so that it is he who fills the table.

To move the drawing location during drawing, using translate().

https://processing.org/reference/translate_.html

To isolate that move and reset it afterwords – so that it does not affect later drawing – use this:

pushMatrix();
translate(x, y);
// drawing
// commands
// here
popMatrix();

https://processing.org/reference/pushMatrix_.html

To load data from a CSV, either:

  1. use loadStrings, then split() – as @GoToLoop explained above
  2. use loadTable()

Thank you very much for helping me move the table. And for the csv, knowing that I have my table which is already done what is simpler: loadStrings, then split () or loadTable () ?

loadStrings is simpler. Table is more powerful, but much more complex.

ok i try and i tell you if it works.

I just have one last little problem. The loadStrings () step works, but I can not get the split () working because there are errors. I show you the code of this void.

void dessinerTableau() {
   textFont(police, 30); 
  String[] professors_absent = loadStrings("Liste.csv");
 String[] profs = split([]professors_absent, ' ');
  for (int line = 0; line < professors_absent.length; line ++) {
    for (int column = 0; column < 3; column++) {
      int x = column*200;
      int y = line*65;
          
      pushMatrix();
        translate(displayWidth/1.96, displayHeight/2.4);
        fill(255);
      stroke(0);
      strokeWeight(2);
      rect(x,y,displayWidth/5, displayHeight/10);
      popMatrix();
      
      String text_affiche = "";
      
        if (column == 0) { 
          text_affiche = professors_absent[line];
        }
        if (column == 1) { 
          text_affiche = subject[line];
        }
        if (column == 2) { 
          text_affiche = schedule[line];
        }
        
        fill(0);
        noStroke();
        text(text_affiche,x+650,y+400);
      }
    }
  }

You need to say exactly what the error text is, and briefly explain how you tried to fix them.

Without the line String[] profs = split([]professors_absent, ' '); there are no errors but as soon as I add this line it gives me 2 errors:

  • Error on “(” Consider adding “byte”
  • Syntax erron on(s), misplaced construct(s)

Concretely reading the page split () I do not understand because in the examples none reads a file csv. So, I still tried doing with this line but there are errors.

The first thing I recommend that you do is read the tutorial on arrays - https://processing.org/tutorials/arrays/
That should help you find some of the errors in your code. Please update us after you’re done.

I just search the site and I was redirected to the necessary functions but I did not understand the function split ().

Can you post a snippet of your csv file, like the first 20 lines?

You can check this post: https://forum.processing.org/two/discussion/28014/max-value-in-an-array#latest

final int ROW0=0;
final int COL_TIME=0;
final int COL_INTENSITY=1;

Table table;
void setup() {

  table=loadTable("data.csv", "header");

  table.setColumnType("Time", Table.INT);
  table.setColumnType("Intensity", Table.FLOAT);

  table.sortReverse("Intensity");

  float  maxValue = table.getFloat(0, "Intensity");

  int time = table.getInt(ROW0, COL_TIME);
  float intensity = table.getFloat(ROW0, COL_INTENSITY);
  println("Max value is " + intensity +" at time " + time  );

}

A small data set:

Time,Intensity
10,0.2
20,0.4
30,0.4
40,0.3
50,0.6
60,0.6
70,0.9
80,0.7
90,0.5
100,0.5
110,0.3
120,0.1
130,0.3
140,0.5
150,0.6
160,0.4
170,0.3
180,0.1
190,0.1
200,0.2
210,0.2
220,0.3
230,0.1
240,0.2

Save the data set inside your data folder in a file called data.csv. LoadTable() could probably be better as it implements all the data handling for you. However, you need to become familiar with its functions and it might not work if you need to change the structure of the table. I recommend you check examples provided and accessed through the PDE or check https://processing.org/examples/loadsavetable.html

Kf

|Name|Subject|Schedule|
|COUTURE|Math|M1-M2|
|MINOUS|English|S2|
|HERVE|Spanish|S1|
|BAUCHE|PE|S3|
|MUTSCHELE|Biology|M4|

I specify that this csv file can only contain one teacher as more than 5 teachers.