Current section
Files
Jump to
Current section
Files
README.md
# tysv
[](https://hex.pm/packages/tysv)
[](https://hexdocs.pm/tysv/)
`tysv` is a streaming parser for CSV files. It parses bytes (BitArray) instead of strings which makes it significantly faster than `gsv`.
The `reader` module covers all the functionality regarding reading and parsing CSV, and `writer` module exports the CSV from typed values. Use `reader.from_bits` for parsing small CSV text already loaded in a variable, or `reader.from_fs` to stream parsed records from a CSV on the disk without starving your RAM.
The API is designed heavily around `use` statements, so it can be a bit overwhelming to understand it by simply overviewing the documentation. Instead, I'd recommend perusing the examples below which cover most of the API.
## Reading the CSV
```gleam
import gleam/list
import tysv/byte_writer as bw
import tysv/reader
import tysv/writer
// Some complex CSV that needs to be parsed.
const before = <<
"# Comments are always skipped.
Flavor;;;Price;Weight;Calorie;yada;yada
Vanilla;;;23;0.25;0.33;tip;toe
Strawberry;;;24;0.75;0.45;wibble;wobble
Mango;;;34;1.75;0.12;humpty;dumpty",
>>
// The expected parsed output.
const rows = [
#("Vanilla", 23, [0.25, 0.33], [<<"tip">>, <<"toe">>]),
#("Strawberry", 24, [0.75, 0.45], [<<"wibble">>, <<"wobble">>]),
#("Mango", 34, [1.75, 0.12], [<<"humpty">>, <<"dumpty">>]),
]
pub fn read_csv() {
// Define the parser in the reader
let reader =
reader.new({
// We can also parse the flavor as `BitArray` using `reader.bits`
use flavor <- reader.string
// Skip next two columns
use <- reader.skip(2)
// Read Int
use price <- reader.int
// Read back to back `Float` in a `List`
use vals <- reader.multiple(2, reader.float)
// Get rest of the values as `List(BitArray)`
use rest <- reader.rest
reader.success(#(flavor, price, vals, rest))
})
// Skip the header if needed.
|> reader.skip_header(True)
// Column separator is <<,>> by default, but customizable.
|> reader.sep(<<";">>)
// Use `reader.from_fs` for FileStream, and `reader.from_bits` for `BitArray`.
|> reader.from_bits(before)
assert rows == reader |> reader.read([], list.prepend) |> list.reverse
// Alternatively:
assert rows
== {
use all, row <- reader.read(reader, [])
[row, ..all]
}
|> list.reverse
}
```
## Writing the CSV
```gleam
// What we want after processing the CSV
const after = <<
"Flavor,Price,Weight,Calorie
Vanilla,23,0.25,0.33
Strawberry,24,0.75,0.45
Mango,34,1.75,0.12",
>>
pub fn write_csv() {
// Build CSV from streaming values
let bw =
writer.new(fn(row) {
let #(flavor, price, vals, _) = row
[
// Convert string to BitArray
writer.string(flavor),
// Convert Int to BitArray
writer.int(price),
// Convert List(Float) to List(BitArray)
..list.map(vals, writer.float(_, 2))
]
})
|> writer.header([<<"Flavor">>, <<"Price">>, <<"Weight">>, <<"Calorie">>])
|> bw.new
assert after == rows |> list.fold(bw, bw.write) |> bw.finish_as_bits
}
```
Further documentation can be found at <https://hexdocs.pm/tysv>.
## Development
```sh
gleam test # Run the tests
```
## Provenance
This project only contains artisanal code, i.e., not AI generated.
## LICENSE
`tysv` is made available under the terms of the EUPL-1.2 License.