CSV module¶
This page explains what the CSV module is and when to use it.
The CSV module processes delimited text files. It loads a file into an in-memory table, or works through a large file in batches to filter rows, save them to disk or send them to a kdb+ tickerplant or the KX Database Service (DB Service). The module infers the type of each column from a sample of rows, so you don't have to write the type string that 0: needs.
The module is written in q, so there is nothing to compile. It works with KDB-X 5.0 or later.
Load the module¶
csv is a reserved keyword in q, so you can't use it as a name. Load the module under another name, such as .csv:
.csv:use`kx.csv
t:.csv.read `:trades.csv
What the module provides¶
readloads a file into a table. Optional arguments set the delimiter and options such as the rows to load, the columns to keep, lines to skip (titles, totals and comments) and type overrides.- Several files. A pattern as the file name, with the wildcards of
like, loads the matching files as one table. The module infers the types once from all of them. - Files from a URL. A string that starts with
http://orhttps://is downloaded and loaded like a file. - Compressed files. The module unzips gzip files, also at a URL, as it reads them.
- Date and time formats. The
formatsoption gives the format of a column, such as%d-%b-%Y, for dates and times that the rules don't recognize. inferreturns the type the rules picked for each column, and why, so you can check it before you load.peekreturns the rows thatinfersamples, to look around in a file.rejectslists the values that don't parse as the type of their column, and the lines with the wrong number of fields. Thestrictoption turns such values into an error.- Batches. The
batchsizeoption loads a file that is too large for memory a batch at a time, andpostProcessfilters or saves each batch.
Type rules¶
The module types each column with an ordered list of numbered rules, grouped into sets. The basic rules always run and cover numbers, dates, times, timestamps, booleans, GUIDs, IP addresses and text. The rules option switches on more sets:
extendedadds guesses from plain digits, such as20240115.financialadds capital markets formats, such as bond prices and checked instrument identifiers like ISIN and CUSIP.
The module also reads thousands separators, ISO 8601 timestamps with a time zone and 12-hour times. You can add a new domain as its own rule set without changing the others.
The basic and extended rules come from csvutil.q, the long-standing KX CSV loading utility. The reference includes a guide for migrating from it.
Use cases¶
The CSV module is most useful for:
- Exploring unknown files. Check the inferred types and look at sample rows before you load a file.
- Loading files with messy formats. Read files with titles, totals, comments, placeholders for nulls, or dates in several formats.
- Loading files too large for memory. Process a file in batches and save each batch to a database.
- Finding bad data. List the values that don't fit the type of their column.
To get per-column statistics of a loaded table, use the Tbl module.
Further information¶
Refer to the CSV GitHub repository for: