michalmonday / CSV-Parser-for-Arduino

It turns CSV string into an associative array (like dict in python)
MIT License
57 stars 12 forks source link

isolate row with parsing_row_by_row_sd_card #31

Open stef-ladefense opened 1 month ago

stef-ladefense commented 1 month ago

hi,

i use your exemple parsing_row_by_row_sd_card.

I have the display of the lines contained in my csv and number of lines in csv. but then how can I retrieve only the values ​​of a line #n?

not found this

thank

Stef

michalmonday commented 1 month ago

Hello, sadly there's no supported way to do that. It only could be done by parsing and ignoring previous lines. For example like this:

void ignore_n_lines(CSV_Parser &cp, int n) {
    for (int i = 0; i < n; i++) {
        if (!cp.parseRow()){
            Serial.println("ERROR: Failed to ignore " + String(n) + " lines. Stopped at index " + String(i));
            return;
        }
    }
}

With the setup function modified like this, in this case it ignores the first 3 lines:

// parseRow calls feedRowParser() continuously until it reads a 
  // full row or until the rowParserFinished() returns true
  int row_index = 3;

  // WARNING: String indexing can't be used here because the header was not supplied to the cp object yet.
  // int32_t *ids = (int32_t*)cp["Index"];
  // char **customer_ids = (char**)cp["Customer Id"];
  // char **emails = (char**)cp["Email"];

  int32_t *ids = (int32_t*)cp[0];
  char **customer_ids = (char**)cp[1];
  char **emails = (char**)cp[9];

  ignore_n_lines(cp, row_index);
  while (cp.parseRow()) {
    // Here we could use string indexing but it would be much slower.
    // char *email = ((char**)cp["Email"])[0];

    int32_t id = ids[0];
    char *customer_id = customer_ids[0];
    char *email = emails[0];

    Serial.print(String(row_index) + ". customer_id=" + String(customer_id));
    Serial.print(", id=");
    Serial.print(id, DEC);
    Serial.print(", email=");
    Serial.println(email);
    row_index++;
  }