1

Using the Polars Rust API, is it possible to create a DataFrame directly from a CSV string / reader while specifying options such as the separator?

Currently, I'm working around this by saving the string to a temporary path and reading it using LazyCsvReader::new("tmp_path.csv").

In the eventual use case, (potentially large) CSV data is received via e.g. a request.

use anyhow::Result;
use polars::prelude::*;

fn main() -> Result<()> {
    let csv_str = "name|age|city
Alice|30|New York
Bob|25|London";

    // Writing it to a file, but I'd prefer to read the CSV data directly.
    std::fs::write("tmp.csv", csv_str)?;
    let df = LazyCsvReader::new("tmp.csv").with_separator(b'|').finish()?.collect()?;

    // Also tried `CsvReader`, though I couldn't figure out how to make it work with a custom delimiter.
    /* let cursor = std::io::Cursor::new(csv_str);
    let df = CsvReader::new(cursor).finish()?; */

    println!("{df}");

    Ok(())
}
2
  • 1
    What is your rust-polars version Commented Nov 30, 2024 at 23:04
  • Current latest: 0.44 Commented Nov 30, 2024 at 23:11

1 Answer 1

2

You can use the into_reader_with_file_handle method of the CsvReadOptions struct to creates a CSV reader using a file handle. Use the map_parse_options to set the custom separator.

use polars::prelude::*;

fn main() {
    let csv_str = "name|age|city
Alice|30|New York
Bob|25|London";
    let handle = std::io::Cursor::new(csv_str);

    let df = CsvReadOptions::default()
        .with_has_header(true)
        .map_parse_options(|parse_options| parse_options.with_separator(b'|'))
        .into_reader_with_file_handle(handle)
        .finish()
        .unwrap();
    println!("{:?}", df);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.