Read data from csv file

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
        self.date = date
        self.time = time
        self.abdomen_length = abdomen_length
        self.thorax_length = thorax_length
        self.head_length = head_length

def setup():
    size(480, 240)
    noLoop()
    background(255)
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))
        t_data.append(beetle)
    # display the data
    fill(0)
    text("Eyed Elaters: Specimen Numbers and Head Lengths (cm)", 20, 20)
    for beetle in t_data:
        fill(255)
        bar_length = beetle.head_length * 4
        rect(20, (beetle.specimen_number + 1) * 32 + 10, bar_length, 20)
        fill(0)
        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_wildwood_june_25_2018_02_43_pm

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

2 Likes