Execute formulas In CSV files
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-12-05 16:52:51 +01:00
csvx added tests, fixed bugs 2025-12-05 16:52:51 +01:00
examples added tests, fixed bugs 2025-12-05 16:52:51 +01:00
tests added tests, fixed bugs 2025-12-05 16:52:51 +01:00
.gitignore initial version 2025-12-04 14:29:55 +01:00
LICENSE initial version 2025-12-04 14:29:55 +01:00
pyproject.toml added tests, fixed bugs 2025-12-05 16:52:51 +01:00
README.md interpret ranges as dataframes, new functions 2025-12-05 15:14:26 +01:00
uv.lock initial version 2025-12-04 14:29:55 +01:00

csvx: execute formulas in CSV files

csvx interprets formulas inside a CSV file and output a fully evaluated CSV. For example, file examples/prices.csv contains formulas (spaces added for readability):

item , price , quantity , total
Egg  ,   0.9 ,       12 , =B2*C2
Spam ,   2.5 ,        3 , =B3*C3

It can be printed with column names and row numbers using csvx -p examples/prices.csv:

      A      B       C          D
 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
  1   item   price   quantity   total
  2   Egg    0.9     12         =B2*C2
  3   Spam   2.5     3          =B3*C3

Running csvx examples/prices.csv produces an evaluated table (spaces added for readability):

item , price , quantity , total
Egg  ,   0.9 ,       12 ,  10.8
Spam ,   2.5 ,        3 ,   7.5

Note that, as above, csvx expects no headers/index in the CSV file. Column names A, B, etc., and row numbers 1, 2, etc., are implicit.

Disclaimer

This project started as a proof of concept to measure how easily eval allows to implement formulas evaluation with the dynamic fetching of cell names from a custom dict class. Indeed, evaluating =B2*C2 as above is just a call to eval("B2*C2", d) with d being a custom dict subclass able to fetch keys B2 and C2 from the actual table and evaluate their values.

Then, having cell ranges required to introduce a class to wrap cell values while keeping track of their locations, which allowed to implement ranges with operator @. This introduced a lot of boilerplate code. From that, the project derived to something cleaner and a bit more structured, but this was not the goal at the beginning.

Consequently: expect missing features, bugs, undocumented aspects, etc.

Security warning

Using eval introduces a huge security issue: if untrusted CSV files are to be evaluated, then untrusted Python code is to be executed. Just consider that running csvx unknown.csv is just as secure as running python unknown.py, that is, not secure at all.

Installation

  • git clone the repo or download the code
  • pip install . from the project directory

Command line interface

Running csvx -h provides the list of options:

  • -o, --out write result to given file instead of stdout
  • -m, --mod load custom functions from Python module (see below)
  • -p, --print print CSV with column/row indexes and exit
  • -f, --functions show available functions with their documentation and exit
  • -d, --debug print full exception traceback en error

Formulas

A cell that starts with = is considered as formula and will be evaluated. Formulas are written as Python expressions, with cell names used as identifiers written as usual. Cells ranges are specified as in A1@A10 (with @ instead of : because the latter would not be valid Python). Such a range will be evaluated as a dataframe with the same columns/index as the range but with the cells evaluated.

Functions can be used, the list of available functions can be displayed by calling csvx -f. For instance, for sum it writes:

sum(*values: Val | CellRange) -> Val: sum values

Cell ranges are iterated over. For instance, sum(A1, B2@B4, C2@D4) is the same
as sum(A1, B2, B3, B4, C2, C3, C4, D2, D3, D4).

This gives the Python signature of the function, with Val being the type for cell values, and CellRange being a pandas dataframe of cell values. Here, sum accepts an arbitrary number of either:

  • single values, originated from constants or cell names like A1
  • 1-dimension dataframe of values, originated from 1-dimension cell ranges like B2@B4
  • 2-dimension dataframe of values, originated from 2-dimension cell ranges like C2@D4

csvx has convenient functions allowing to get cells relatively to the current one. For instance, in file examples/rel_prices.csv we have (spaces added for readability):

item , price , quantity , total
Egg  ,   0.9 ,       12 , =rcol(-2)*rcol(-1)
Spam ,   2.5 ,        3 , =rcol(-2)*rcol(-1)

Using function rcol(-2), one can get the cell that is two columns on the left of the current one, which allows to have the same formula throughout column total. Note that this function returns a Cell instead of a Val, this allows it to be used in ranges, for instance, rcol(-2)@rcol(-1) is valid.

Adding new functions

File examples/__init__.py defines a function ifte that implements an if-then-else. It can be used as a template to add more functions with proper documentation.

from csvx import Val

def ifte(self, test: bool, then: Val, orelse: Val) -> Val:
    """if/then/else

    Return `then` if `test` is True, `orelse` otherwise.
    """
    return then if test else orelse
  • Val, the type of cell values, is imported from csvx module
  • function ifte has a first parameter self because the function will be added to the class Table (see below) as a method, this allows in particular to call self.iter_values(values), when values is typed as in the sum function, to extract each individual values in an heterogeneous mixture of Val or CellRange
  • the parameters types and return type are indicated
  • the docstring has a short description right in the first line, followed by a longer description if needed, after an empty line

When all these constraints are met, calling csvw -m examples -f will properly show the documentation for the added functions.

Function ifte above is used in file examples/marks.csv that can be evaluated running csvx -m examples examples/marks.csv.

Using csvx as a library

Just import csvx in your code, the classes are:

  • Table: main class that wraps a pandas.DataFrame and implements cells evaluation
  • Cell: wraps a Val to track which cell it comes from
  • CID: a cell identifier
  • CSVError: exception raised on CSV evaluation errors
  • Val: the type of cell values

Documentation is partial, in the docstrings and comments.

Licence

This package is (C) 2025 Franck Pommereau franck.pommereau@univ-evry.fr and released under the MIT Licence, see LICENCE for details.