~/datapy v0.2.0 on pypi

corey@mccarty.dev:~/datapy$

What a query can do

The examples on this page are run by the test suite against people.csv:

name,age,job,zip
Ann,49,engineer,01234
Bob,7,student,30301
Cy,101,retired,99501

They use the Python API, but every statement works unchanged on the command line.

>>> import datapy
>>> people = datapy.open("people.csv")
>>> def show(statement):
...     return [tuple(row) for row in people.query(statement)]

Statements

A query is one select statement naming one file. It is run by SQLite, so where, order by, group by, limit, expressions and SQLite's built-in functions all work.

>>> show("select name, age from people.csv where age > 40 order by age desc")
[('Cy', 101), ('Ann', 49)]
>>> show("select count(*), max(age) from people.csv")
[(3, 101)]
>>> show("select upper(name) from people.csv where job like '%ent' limit 1")
[('BOB',)]

Anything other than a select is refused, so a query can never change your file or write anything else to disk.

>>> show("delete from people.csv")
Traceback (most recent call last):
    ...
datapy.DatapyError: only select statements are supported

Columns

Refer to a column by its header name or by its position. $1 is the first column.

>>> show("select $1, job from people.csv where $2 < 18")
[('Bob', 'student')]

For a file without a header line, open it with header=False (-H on the command line). Its columns are then named $1, $2 and so on.

Header names are matched without regard to case, and a file with two columns of the same name is rejected. A header name containing spaces or punctuation needs double quotes in the statement: select "first name" from ....

Column types

Every value in a text file starts as text. Datapy looks at the first 1,000 rows of each column and treats it as a number only when every non-empty value there is one:

Values in the column Treated as
all whole numbers, e.g. 7, -12 integer
all numbers, some with a decimal point or exponent, e.g. 2.5, 1e3 real
anything else text

So comparisons on a numeric column are numeric. As text, "7" would sort after "49":

>>> show("select name from people.csv where age > 40 order by age")
[('Ann',), ('Cy',)]

A number with a leading zero, such as a zip code, is kept as text so the zero is not lost:

>>> show("select zip from people.csv where name = 'Ann'")
[('01234',)]

If a column only looks numeric in its first 1,000 rows, keep it as text with -t on the command line or text= in Python:

>>> with datapy.open("people.csv", text=["age"]) as as_text:
...     [tuple(row) for row in as_text.query("select age from people order by age")]
[('101',), ('49',), ('7',)]

An empty field in a numeric column is null. A row with fewer fields than the header has null for the missing ones; a row with more fields is an error.

Naming the file

The file goes in the from clause. Put the name in double quotes if it contains spaces. The name - means standard input.

With datapy.open() the file is already chosen, so the from clause may use the path it was opened with, the file name, or the name without its extension:

>>> show("select count(*) from people")
[(3,)]
>>> people.close()

Limits