Skip to content
ccoffey edited this page Jun 24, 2011 · 34 revisions

What is sql4csv

sql4csv is a python library that offers an SQL "like" interface for .csv files.

What else do I need?

sql4csv is powered by pyparsing so you need to install pyparsing. pyparsing is an amazing open source project that offers an alternative approach to creating and executing simple grammars.

Examples

For clarity's sake, each section of examples will contain a data set, some example queries and the output of each query.

dataset_0

# Import sql4csv and create a queryable data source from a .csv file.
from novacode import sql4csv
ds = sql4csv('test.csv')
# Extract two columns from the data set.
ds.query('select $fname, $lname')
# Extract two columns from the data set but this time rename them.
ds.query('select $fname as $first_name, $lname as $last_name')
# Combine two columns from the data set.
ds.query('select #0($fname, $name) as $fullname', [lambda a, b: '%s %s' % (a, b)])

sql4csv is very flexible, each of the following queries produces the same result. You choose how you wish to write your queries, sql4csv won't impose to many restrictions on you.

# Extract two columns and their combination from the data set.
ds.query('select $fname, $lname, #0($fname, $lname) as $fullname', [lambda a, b: '%s %s' % (a, b)])
ds.query('select #0($fname as $fname, $lname as $lname) as $fullname', [lambda a, b: '%s %s' % (a, b)])

Clone this wiki locally