How to read local csv file as ndarray using Rust

162 views Asked by At

I recently went through Rust documentation and found that you can directly read CSV as ndarray. The only thing that makes me concerned, looks like you need to know the shape of the file you read in advance.

Here's the code from the Rust docs:

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(())
}

It was taken from here.

I tried this:

fn read_csv(path_to_file: &str) -> Result<Array2<u64>, Box<dyn Error> > {
    let file = File::open(path_to_file);
    let mut reader = ReaderBuilder::new().has_headers(true).from_reader(file);
    Ok(reader.deserialize_array2((210, 2)))
    }

 let array = read_csv("data/train_data_5exp.csv")?;
 println!("{:?}", array);

For some reason, this function doesn't read even with the provided shape of the file.

The document example is not quite working for me since I want to read CSV with an unknown shape. How can I make a function?

Thank you in advance.

1

There are 1 answers

0
user5417363 On

you just need to replace this

let array_read: Array2<u64> = reader.deserialize_array2((2, 3))?;

with this

let array_read: Array2<u64> = reader.deserialize_array2_dynamic()?;