Tutorial

The Short Version

Sometimes an example works best. Suppose you have the following csv file:

Node;Load case;Moment;Shear
1;LC1;123;23
1;LC2;100;-23
1;LC3;43;234
2;LC1;-123;23
2;LC2;-100;-23
2;LC3;-43;234
3;LC1;123;-23
3;LC2;100;-23
3;LC3;43;-234

In order to parse this file, all you have to do is to define a class that declares the structure of each csv’s rows:

from csvmodels import RowModel, StringColumn, IntegerColumn, FloatColumn

class MyRow(RowModel):
    node = IntegerColumn()
    load_case = StringColumn()
    moment = FloatColumn()
    shear = FloatColumn()

    _dialect = {
        "has_header_row": True,
        "delimiter": ";"
        }

As you can see the class definition is rather simple. You just add a class attribute for each column of the csv file. While doing so, you also declare the type of each column (string, integer, float etc).

Note

Attribute _dialect is a special one. Since csv files come into many variations (or dialects if we stick to the terminology of the standard library’s csv module) it is necessary to provide some additional info. This is done through the _dialect attribute whose type must be a dictionary. In case it is not provided, it defaults to an empty dict. _dialect supports all the keywords arguments that csv.Dialect supports plus some additional ones that are specific to csvmodels.

After the class declaration there is really not much more to do. csvmodels provides a Reader class that, in reality, is just a wrapper to standard library’s csv.reader function. All you have to do is to create a Reader object and iterate through it to get the csv data row by row:

from csvmodels import Reader
from models import MyRow

filename = "test.csv"
with open(filename, "r") as f:
    reader = Reader(MyRow, f)

    for row in reader:
        # Each row object is an instance of MyRow
        print("Node: %d, load case: %s, Moment=%.1f" % (row.node, row.load_case, row.moment))

results in:

Node: 1, load case: LC1, Moment=123.0
Node: 1, load case: LC2, Moment=100.0
Node: 1, load case: LC3, Moment=43.0
Node: 2, load case: LC1, Moment=-123.0
Node: 2, load case: LC2, Moment=-100.0
Node: 2, load case: LC3, Moment=-43.0
Node: 3, load case: LC1, Moment=123.0
Node: 3, load case: LC2, Moment=100.0
Node: 3, load case: LC3, Moment=43.0

In case you need to process the data column-by-column, Reader objects provide method get_columns() that returns a dictionary with keys the class’ attributes and values the csv columns as a list:

filename = "test.csv"
with open(filename, "r") as f:
    reader = Reader(MyRow, f)
    csv_data = reader.get_columns()

for key, values in csv_data.items():
    print(key, ":", csv_data)

results in:

node : [1, 1, 1, 2, 2, 2, 3, 3, 3]
moment : [123.0, 100.0, 43.0, -123.0, -100.0, -43.0, 123.0, 100.0, 43.0]
shear : [23.0, -23.0, 234.0, 23.0, -23.0, 234.0, -23.0, -23.0, -234.0]
load_case : ['LC1', 'LC2', 'LC3', 'LC1', 'LC2', 'LC3', 'LC1', 'LC2', 'LC3']

And that was it! :)

In the next section we will see how we can use csvmodels in order to validate our data.

Project Versions

Table Of Contents

Previous topic

Intro

Next topic

Module csvmodels.base – Base

This Page