# 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:`](../../ref/file-text.md) 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`:
```q
.csv:use`kx.csv
t:.csv.read `:trades.csv
```

## What the module provides

- **`read`** loads 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://` or `https://` 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 `formats` option gives the format of a column, such as `%d-%b-%Y`, for dates and times that the rules don't recognize.
- **`infer`** returns the type the rules picked for each column, and why, so you can check it before you load.
- **`peek`** returns the rows that `infer` samples, to look around in a file.
- **`rejects`** lists the values that don't parse as the type of their column, and the lines with the wrong number of fields. The `strict` option turns such values into an error.
- **Batches.** The `batchsize` option loads a file that is too large for memory a batch at a time, and `postProcess` filters 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:

- `extended` adds guesses from plain digits, such as `20240115`.
- `financial` adds 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](../tbl/overview.md).

## Further information

Refer to the CSV GitHub repository for:

- [CSV module reference](https://github.com/KxSystems/csv/blob/main/docs/reference.md){target=_blank}
- [Installation guide](https://github.com/KxSystems/csv/blob/main/docs/install.md){target=_blank}
