Skip to content

Loading CSV#

CSV are a way to store "tabular" data. If you have ever worked with LibreOffice Calc or with Excel, both can produce CSV files, too. The name simply means "comma separated values" and this will probably be enough information to immediately understand what's going on in the following example. Mind you, CSV files are also just plain text files. We just usually store them in files ending in .csv.

list.csv
id,name,email
487538,Anna Tashiro,Anna.Tashiro@stud.example
838535,Chiyoko Izumi,Chiyoko.Izumi@stud.example
766334,Edelgard Stock,Edelgard.Stock@stud.example
469408,Evdokia Gerasimenko,Evdokia.Gerasimenko@stud.example
174357,Hauke Haack,Hauke.Haack@stud.example
356416,Hisayoshi Narita,Hisayoshi.Narita@stud.example
315365,Ilnur Malakhov,Ilnur.Malakhov@stud.example
522294,Jasmin Krause,Jasmin.Krause@stud.example
600723,Kaori Tajiri,Kaori.Tajiri@stud.example
807134,Kenji Serizawa,Kenji.Serizawa@stud.example
984726,Kurban Petrov,Kurban.Petrov@stud.example
217354,Medina Emelyanova,Medina.Emelyanova@stud.example
770286,Meta Benz,Meta.Benz@stud.example
224003,Mikio Shoji,Mikio.Shoji@stud.example
134906,Nicola Mertens,Nicola.Mertens@stud.example
514127,Rainer Kern,Rainer.Kern@stud.example
262030,Sigrid Klinger,Sigrid.Klinger@stud.example
959679,Tatsuhiko Tsujimoto,Tatsuhiko.Tsujimoto@stud.example
765275,Tatyana Ulyanova,Tatyana.Ulyanova@stud.example
963685,Valeria Rybakova,Valeria.Rybakova@stud.example
998174,Yakov Doroshenko,Yakov.Doroshenko@stud.example    

Randomly Generated

Don't worry, these names were all randomly generated! While there are probably real people in the world that might have these names, too, this is not a real list of real people!

CSV puts each entry or dataset on its own line. The lines can consist of a lot of fields - the columns of your table. In this example, we're using , comma as field separator. This is not mandatory, there are a lot of common choices (; semi colon, \t tab or space are also quite common).

Copy and Paste

Try copying the lines above and paste them into LibreOffice Calc, Excel, Numbers or Google Sheets. All of these programs should recognize that this is tabular data automatically! You may need to configure a few things like field separator or encoding.

import dialog of libreoffice calc

This particular example has the field names in the first row, but that's not mandatory. I like doing this, because it helps me recognize the data later on. CSV can do more things, but as this is not a really standardized format, I will leave it at this level of complexity for now.

Using CSV in Processing#

As we've seen, Processing can read a file into lines quite easily and this is almost all we need to use CSV. Let's combine it with splitting a string.

loading_csv.pde hl_lines=
String[] students;

void setup() {
    size(500, 500);
    students = loadStrings("list.csv");
}

void draw() {
    background(0);
    float yPosition = 50;
    for (String student : students) {
        String[] fields = student.split(",");
        text(fields[0], 40, yPosition);
        text(fields[1], 100, yPosition);
        text(fields[2], 250, yPosition);
        yPosition += 20;
    }
}

We're loading the whole file into a String array just like we did for regular text files. We're then using a for-each loop to go over each of the lines, we're splitting that line by a , comma and then display the individual parts. This should render the list in a somewhat nice way.

the same student-like data rendered in Processing

Errors Possible

The code above assumes that each of the lines has at least those three fields. It would easily break if there was a blank line. It would be safer to check if fields[1] and fields[2] even exist before using them in text(). But that would have distracted from the main part: reading text from a file and splitting it.