# Creating an empty Table object in Python mode (and some other hidden "data" classes)

**URL:** <https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121>\
**Category:** Processing.py\
**Created:** [November 1, 2020, 9:36pm UTC](https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121 "2020-11-01T21:36:08Z")\
**Posts on this page:** 4\
**Page:** 1

<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:** [November 1, 2020, 9:36pm UTC](https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121/1 "2020-11-01T21:36:08Z")

</div>

Another documentation gap that drives me mad is that the Python equivalent of an example like the (Java) `Table t = new Table()` is missing.

[Table](https://processing.org/reference/Table.html) is missing from the [Py.Processing.org/reference](http://Py.Processing.org/reference) and nobody tells you how to import the needed constructor with `from processing.data import Table` ☹

So here you go:

```python
from processing.data import Table

def setup():
  table = Table()
  
  table.addColumn("id")
  table.addColumn("species")
  table.addColumn("name")
  
  newRow = table.addRow()
  newRow.setInt("id", table.getRowCount() - 1)
  newRow.setString("species", "Panthera leo")
  newRow.setString("name", "Lion")
  
  saveTable(table, "data/new.csv")

```

For the curious, there are some other Python\<-\>Java compatibility-helping objects hidden on this `data` module.

```auto
import processing
print(dir(processing.data))
['DoubleDict', 'DoubleList', 'FloatDict', 'FloatList', 'IntDict', 'IntList', 'JSONArray', 'JSONObject', 'JSONTokener', 'LongDict', 'LongList', 'Sort', 'StringDict', 'StringList', 'Table', 'TableRow', 'XML', ' __name__']

```

Why not use the nice built-in Python `csv` module you might ask?

```auto
import csv
with open('names.csv', 'w') as csvfile: # Python 2 has no encoding on open()
        fieldnames = ["species", "name"]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerow({"species" : "Panthera Leo", "name" : u"Léo the Lion"})

```

I had problems with unicode strings, (non-ASCII characters) encoding it writes the file as ISO-885914 and not UTF-8 I would prefer.

---

<div class="post-metadata">

**Author:** ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)\
**Post date:** [November 2, 2020, 12:58pm UTC](https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121/2 "2020-11-02T12:58:43Z")

</div>

> [@villares](#):
>
> `with open('names.csv', 'w') as csvfile:`

You can set the encoding in open()-function

```auto
with open('names.csv', 'w', encoding="utf-8") as csvfile:

```

---

<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:** [November 2, 2020, 1:13pm UTC](https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121/3 "2020-11-02T13:13:00Z")

</div>

Hi @SomeOne!

This is Python 2.7 (Jython) and so `open()` has no `encoding` keyword argument ☹

A have also tried to `import io` and then used `io.open()` that is closer to Python 3 (so I tried to make it work with `encoding=`) but it didn’t work either for me 😕 … If anyone manages to make `io.open()` work with `csv.DictWriter` I’d be really glad. I have studied the docs, they show some enconding wrapper techiques for `csv` but I couldn’t manage it.

---

<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:** [May 1, 2021, 11:38pm UTC](https://discourse.processing.org/t/creating-an-empty-table-object-in-python-mode-and-some-other-hidden-data-classes/25121/4 "2021-05-01T23:38:22Z")

</div>

> [@villares](#):
>
> A have also tried to `import io` and then used `io.open()` that is closer to Python 3 (so I tried to make it work with `encoding=` ) but it didn’t work either for me 😕 … If anyone manages to make `io.open()` work with `csv.DictWriter` I’d be really glad. I have studied the docs, they show some enconding wrapper techiques for `csv` but I couldn’t manage it.

I think I have the solution for the UTF-8 on Python 2 now! It is:

`from codecs import open`

An example:

```python
from __future__ import unicode_literals, print_function
import csv
from codecs import open

# Define data
data = [
    (1, "maçã,", 1.0),
    (42, "manga, uva", 2.0),
    (1337, "jaca", -1),
    (0, "kiwi", 123),
    (-2, "Nada.", 3),
]
def setup():
    size(600, 600)
    background(0, 100, 0)
    # textSize(24)
    textFont(createFont('Source Code Pro', 24))

    # Write CSV file
    with open("test.csv", "wt", encoding="utf-8") as fp:
        writer = csv.writer(fp)
        # writer.writerow(["your", "header", "foo"]) # write header
        writer.writerows(data)
    
    # Read CSV file
    with open("test.csv", encoding="utf-8") as fp:
        reader = csv.reader(fp)
        # next(reader, None) # skip the headers
        data_read = [row for row in reader]
        x, y = 20, 20
        for row in data_read:
            for item in row:
                print(item.ljust(12), end='')
                text(item.ljust(12), x, y)
                x += textWidth(item.ljust(12))
            y += 32
            x = 20
            print()

```

UPDATE/WARNING: `csv.DictReader(file)` does not work with `utf-8` because of Jython limitations [13.1. csv — CSV File Reading and Writing — Jython v2.5.2 documentation](https://www.jython.org/jython-old-sites/docs/library/csv.html) `csv.reader()` might be unsafe too ☹
