Mueez Khan
czv for CSV Operations
A collection of libraries for running CSV operations called czv. Built in
Rust, available for Rust, Python, and WebAssembly (JavaScript/TypeScript).
Published: 2024-06-21
Last updated: 2024-06-21
side-project
rust
python
webassembly
javascript
typescript
react
data-engineering
data-analysis
A recent library I've been working on is czv as a side project built with Rust and including bindings for Python and WebAssembly (JavaScript/TypeScript).
Note: The code may be outdated/updated by the time you read this as this post was published when czv was released.
Examples
For the upcoming examples we'll be using the following CSV data: Since there are three libraries, there are three primary ways you may use czv.Rust
Install the crate from crates.io/crates/czv by running: Now let's print out the row count (including the header row) for our CSV data: In the czv Rust library, the builder pattern is recommended. Here are some notes on this example:- The
RowCount
is a Rust struct we consider as our CSV operation so we import it in the first line. - We assign the CSV data to a variable as a
&str
. - The
RowCount::new()
method must be called to generate aRowCountBuilder
. - The
RowCountBuilder
allows us to run multiple methods to pass conditional arguments. In this case we run.file_data(data)
to provide our CSV data (alternatively for a file path usefile_path(path)
) and.include_header_row(true)
to include the CSV data's first row in the total row count. - The
.execute()
method is ran at the end to get the output as aResult<T, CzvError>
. In this caseT
is ausize
(representing the row count) and if.execute()
runs successfully we get the value thanks to the question-mark operator and assign it to our variableoutput
.
Python
Install the package from pypi.org/project/czv by running:Personally I prefer usingNow let's print out the row count (including the header row) for our CSV data: Similar to the czv Rust library'suv
instead ofpip
.
RowCount
builder, we may use a row_count
function directly in Python since we may provide optional parameters and keyword arguments whereas in Rust the builder pattern suffices for this necessity.
WebAssembly (JavaScript/TypeScript)
Install the package from npmjs.com/package/czv-wasm by running: You may replacebun
with the compatible package manager you use (e.g., npm
, pnpm
, yarn
).
I received an error when trying to registerHow you use this library depends on the scenario it's used in as some frameworks make it easier to work with than others. For example I'll use czv-wasm within this page. My website is built with Next.js and this page uses MDX so first I'll try installing the czv-wasm package: Then I'll make a React component based on the web demo example (which is built with Vite and React): The code could be refactored but I'm mainly trying to show the functionality. Since I'm using Next.js I addczv
as the package name since its name is too similar tocsv
, so we useczv-wasm
instead.
"use client"
to render this on the client-side which allows useState
to work. The component imports are from the shadcn/ui library.
I'll add the component to this page's MDX file as so:
Now you may try it out above!
Import a CSV file using the file browser. You may also click the switch to toggle whether you want to include the header (first) row from your CSV file. The row count and column count should be computed by
czv-wasm
within your browser and displayed.
If you'd like to explore more with czv-wasm, docstrings are provided for TypeScript.