Read data from csv file


I am working on a project where I would like to read data from a csv file using python mode. From what I can gather, I need to create a Reader, though I am uncertain how to do so (please note that I am a self taught coder). I am following the reference on the Processing site here (createReader() \ Language (API)), though it says that the example is broken. Also, I only want to access data from 3 columns of the csv file. Sample csv data is here:

 Thursday 2/4/2021 15:39:44	100	126	56
Thursday 2/4/2021 15:39:47	100	92	90
Thursday 2/4/2021 15:39:49	100	92	77
Thursday 2/4/2021 15:39:51	100	92	37
Thursday 2/4/2021 15:39:53	100	92	86
Thursday 2/4/2021 15:39:55	100	92	91

I only want to use data from the last three columns.

Here is what I have so far in my code:

import csv
filename = 'BiometricDataTest.csv'

def setup():
    size(1920, 1080)

    reader = csv.reader(open(filename),quoting=csv.QUOTE_NONNUMERIC)

def draw():
    #read data
    for row in reader:

And that is where I am stuck.


Although unmentioned on Python Mode’s own reference:

Processing’s API got some easier to use CSV methods like loadTable():

Using the Java Mode sketch from this link below as reference:

I did this simplified example which demos how to use loadTable() on Python Mode :

def setup():
    global t
    t = loadTable("data.csv", "header")

    for row in t.rows():
        x = row.getFloat("x")
        y = row.getFloat("y")
        d = row.getFloat("diameter")
        n = row.getString("name")

        print x, y, d, n


I think that the @GoToLoop strategy is the way to go… but just for exploration I think the Python CSV strategy might have failed because of quoting=csv.QUOTE_NONNUMERIC

I made it work like this:

PS: I also added a global reader statement.


Hello, @fleshcircuit,

What would you like to do with the data? Since you created a large canvas, you probably wanted to display it in some fashion.

Here’s an example that simply displays it as text on a small canvas:

import csv
def setup():
    size(300, 240)
def draw():
    # get the data and store it in a list for later use
    t = csv.reader(open("BiometricDataTest.csv"), delimiter="\t")
    t_data = []
    for row in t:
        _day, _date, _time, x, y, z = row
        t_data.append([int(x), int(y), int(z)])
    # do something with the data
    for row, data in enumerate(t_data):
        text("{:03d} {:03d} {:03d}".format(data[0], data[1], data[2]), 20, (row + 1) * 32)


Thanks for the tips everyone! I am using the data to manipulate shapes through an animation, such as altering acceleration and changing colour. I managed to get the upload using table and add it to list, though the code is clunky. I will try out these other suggestions to optimize it.


Since each specimen, or whatever kind of object each row represents, has several pieces of information associated with it, you may benefit from defining a class that can be instantiated to represent each object. This can help you organize your data.

The following assumes that each row represents data concerning a beetle specimen, but you can of course adapt the pattern to the true identity of your objects.

import csv
class Beetle(object):
    def __init__(self, specimen_number, weekday, date, time, abdomen_length, thorax_length, head_length):
        self.specimen_number = specimen_number
        self.weekday = weekday = date
        self.time = time
        self.abdomen_length = abdomen_length
        self.thorax_length = thorax_length
        self.head_length = head_length

def setup():
    size(480, 240)
def draw():
    # get and store the data
    t = csv.reader(open("BiometricDataTest.csv"), delimiter="\t")
    t_data = []
    for specimen_number, data in enumerate(t):
        weekday, date, time, abdomen_length, thorax_length, head_length = data
        beetle = Beetle(specimen_number, weekday, date, time, int(abdomen_length), int(thorax_length), int(head_length))
    # display the data
    text("Eyed Elaters: Specimen Numbers and Head Lengths (cm)", 20, 20)
    for beetle in t_data:
        bar_length = beetle.head_length * 4
        rect(20, (beetle.specimen_number + 1) * 32 + 10, bar_length, 20)
        text("{:d}    {:d}".format(beetle.specimen_number, beetle.head_length), 8, (beetle.specimen_number + 1) * 32 + 25)

Eyed Elater Data

The head lengths are cited in the figure as being in centimeters. Eyed elaters aren’t really that large, though they are a large species among the click beetles.

EDIT (April 10, 2021):

For the record, actual eyed elaters (Alaus oculatus) are typically 25–45 millimeters (1.0–1.8 in) in length. See Wikipedia: Alaus oculatus.


Eyed Elater (Alaus oculatus)
Photo by Sandy Richard
Wildwood State Park, Wading River, NY, USA
June 25, 2018