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

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 is missing from the Py.Processing.org/reference and nobody tells you how to import the needed constructor with from processing.data import Table :frowning:

So here you go:

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.

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?

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.

4 Likes

You can set the encoding in open()-function

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

Hi @SomeOne!

This is Python 2.7 (Jython) and so open() has no encoding keyword argument :frowning:

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 :confused: … 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.

1 Like

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

from codecs import open

An example:

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 csv.reader() might be unsafe too :frowning:

2 Likes