# Read data from csv file

**URL:** <https://discourse.processing.org/t/read-data-from-csv-file/29248>\
**Category:** Processing.py\
**Created:** [April 9, 2021, 4:56pm UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248 "2021-04-09T16:56:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)\
**Post date:** [April 9, 2021, 4:56pm UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/1 "2021-04-09T16:56:17Z")

</div>

Hello,

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)](https://py.processing.org/reference/createReader.html)), 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:

```auto
 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:

```auto
import csv
filename = 'BiometricDataTest.csv'

def setup():
    size(1920, 1080)

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

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

```

And that is where I am stuck.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 9, 2021, 5:51pm UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/2 "2021-04-09T17:51:56Z")

</div>

> [@fleshcircuit](#):
>
> I would like to read data from a csv file using python mode.

Although unmentioned on Python Mode’s own reference:

> **[Reference](https://py.processing.org/reference/)**
>
> Python Mode for Processing extends the Processing Development Environment with the Python programming language.

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

> **[loadTable() / Reference](https://processing.org/reference/loadTable_.html)**
>
> Reads the contents of a file or URL and creates a Table object with its values. If a file is specified, it must be located in the sketch's "data" folder. The filename parameter can also be a URL to a …

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

> <https://github.com/processing/processing-docs/tree/master/content/examples/Topics/Advanced%20Data/LoadSaveTable>
>
> //github.com/processing/processing-docs/tree/master/content/examples/Topics/Advanced%20Data/LoadSaveTable

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

```auto
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

    exit()

```

---

<div class="post-metadata">

**Author:** ![villares](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/villares/32/3166_2.png) [@villares](https://discourse.processing.org/u/villares)\
**Post date:** [April 9, 2021, 9:10pm UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/3 "2021-04-09T21:10:03Z")

</div>

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:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/fc2f07738912cf60bb24ca85fd14fd2749ad92d0.png)

PS: I also added a `global reader` statement.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [April 10, 2021, 12:01am UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/4 "2021-04-10T00:01:26Z")

</div>

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:

```auto
import csv
def setup():
    size(300, 240)
    textSize(24)
    fill(0)
    noLoop()
    background(255)
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)

```

![Biometric](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b3418682213321a667453865208a4ba9dec46aee.png)

---

<div class="post-metadata">

**Author:** ![fleshcircuit](https://avatars.discourse-cdn.com/v4/letter/f/b3f665/32.png) [@fleshcircuit](https://discourse.processing.org/u/fleshcircuit)\
**Post date:** [April 10, 2021, 11:08am UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/5 "2021-04-10T11:08:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [April 10, 2021, 12:22pm UTC](https://discourse.processing.org/t/read-data-from-csv-file/29248/6 "2021-04-10T12:22:27Z")

</div>

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.

```auto
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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/44c9cd09136713828806f4f3e8478690a73a889b.png)

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_](https://en.wikipedia.org/wiki/Alaus_oculatus).

![eyed_elater_wildwood_june_25_2018_02_43_pm](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/8/8e335b95f97ee169370b31e14609ed2b83e09333.png)

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