Convert spreadsheet into academic references (Help!)

Hello! I’m (trying) to write a simple code to transform job references written in a spreadsheet into text. I started with a test spreadsheet converted into CSV, but I’m having some formatting problems: the information always appears accompanied by “;” and I can’t add the required punctuation.
The correct formatting would be: Author1; Author2; Author3. Title. Location: publisher, year.
I really appreciate the help! (Detail: I’m Brazilian, so I had to translate the code for you. If there is any problem understanding, please let me know!)

String[] lines;
String textFormatted = "";

void setup() {
  lines = loadStrings("TS.csv");

  for (String line : lines) {
    String[] columns = line.split(",");

    // Verificando se a coluna está preenchida antes de adicioná-la ao texto formatado
    String author1 = columns.length > 0 && !columns[0].isEmpty() ? columns[0] : "";
    String author2 = columns.length > 1 && !columns[1].isEmpty() ? columns[1] : "";
    String author3 = columns.length > 2 && !columns[2].isEmpty() ? columns[2] : "";
    String title = columns.length > 3 && !columns[3].isEmpty() ? ". " + columns[3] : "";
    String local = columns.length > 4 && !columns[4].isEmpty() ? ": " + columns[4] : "";
    String press = columns.length > 5 && !columns[5].isEmpty() ? ", " + columns[5] : "";
    String year = columns.length > 6 && !columns[6].isEmpty() ? columns[6] : "";

 textFormatted += author1 + author2 + author3 + title + local + press + year + ".\n";

  }

  saveStrings("saida_abnt.txt", new String[]{textFormatted});
}

void draw() {
}

1 Like

Can you please post the first 5 lines of the csv?

1 Like

Did you look at the file using notepad?

Maybe when you used Excel, it imported with ; instead of using,

You can also try export as tsv (separated by tabs)


You could also look at loadTable()

But back to your code I have difficulties to see where you
insert your ; after Author1 for example

But maybe it’s just a version of the code where you tried different things

3 Likes

I assume that the books have different
numbers of authors.

That makes it a bit difficult to handle.

For example if you have 3 authors you work

  • with ; ; .

  • For 2: ; .

  • For 1: .

So that is difficult.

Maybe it’s similar to other parts, when location is missing e.g…

Anyway you can of course simply delete the
superfluous ; in the incoming data using replace().

Chrisir

2 Likes

In the end I used tsv and loadTable, as you suggested. I managed to create conditions for the variable number of authors. Problem solved, thank you!

2 Likes