#csv #data #file-io #ndarray #2d-array #read-write #homogeneous

ndarray-csv

Easily read and write homogeneous CSV data to and from 2D ndarrays

12 releases

Uses old Rust 2015

0.5.3 Oct 25, 2024
0.5.2 May 4, 2023
0.5.1 Apr 25, 2021
0.5.0 Jan 30, 2021
0.3.1 Oct 30, 2018

#114 in Graphics APIs

Download history 762/week @ 2024-08-19 780/week @ 2024-08-26 1355/week @ 2024-09-02 676/week @ 2024-09-09 635/week @ 2024-09-16 1030/week @ 2024-09-23 979/week @ 2024-09-30 545/week @ 2024-10-07 902/week @ 2024-10-14 844/week @ 2024-10-21 717/week @ 2024-10-28 1105/week @ 2024-11-04 4113/week @ 2024-11-11 12310/week @ 2024-11-18 9566/week @ 2024-11-25 12853/week @ 2024-12-02

38,860 downloads per month
Used in 25 crates (11 directly)

MIT/Apache

14KB
246 lines

ndarray-csv

Easily read and write homogeneous CSV data to and from 2D ndarrays.

extern crate csv;
extern crate ndarray;
extern crate ndarray_csv;

use csv::{ReaderBuilder, WriterBuilder};
use ndarray::{array, Array2};
use ndarray_csv::{Array2Reader, Array2Writer};
use std::error::Error;
use std::fs::File;

fn main() -> Result<(), Box<dyn Error>> {
    // Our 2x3 test array
    let array = array![[1, 2, 3], [4, 5, 6]];

    // Write the array into the file.
    {
        let file = File::create("test.csv")?;
        let mut writer = WriterBuilder::new().has_headers(false).from_writer(file);
        writer.serialize_array2(&array)?;
    }

    // Read an array back from the file
    let file = File::open("test.csv")?;
    let mut reader = ReaderBuilder::new().has_headers(false).from_reader(file);
    let array_read: Array2<u64> = reader.deserialize_array2((2, 3))?;

    // Ensure that we got the original array back
    assert_eq!(array_read, array);
    Ok(())
}

This project uses cargo-make for builds; to build, run cargo make all.

To prevent denial-of-service attacks, do not read in untrusted CSV streams of unbounded length; this can be implemented with std::io::Read::take.

License: MIT/Apache-2.0

Dependencies

~3MB
~50K SLoC