#ndarray #multidimensional-array

ndarray-csv

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

11 releases

Uses old Rust 2015

0.5.2 May 4, 2023
0.5.1 Apr 25, 2021
0.5.0 Jan 30, 2021
0.4.1 Mar 6, 2020
0.3.1 Oct 30, 2018

#256 in Parser implementations

Download history 751/week @ 2023-06-06 851/week @ 2023-06-13 1191/week @ 2023-06-20 939/week @ 2023-06-27 1324/week @ 2023-07-04 1083/week @ 2023-07-11 999/week @ 2023-07-18 1235/week @ 2023-07-25 1420/week @ 2023-08-01 814/week @ 2023-08-08 1026/week @ 2023-08-15 852/week @ 2023-08-22 378/week @ 2023-08-29 544/week @ 2023-09-05 1183/week @ 2023-09-12 505/week @ 2023-09-19

2,661 downloads per month
Used in 23 crates (10 directly)

MIT/Apache

14KB
244 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::from(vec![1, 2, 3, 4, 5, 6]).into_shape((2, 3)).unwrap();

    // 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
~47K SLoC