8 releases (4 breaking)

0.5.1 Apr 21, 2024
0.5.0 Jan 13, 2024
0.4.1 Nov 29, 2023
0.4.0 Oct 30, 2023
0.1.0 Sep 5, 2023

#1143 in Encoding

Download history 31/week @ 2024-06-17 36/week @ 2024-06-24 1/week @ 2024-07-01 8/week @ 2024-07-08 32/week @ 2024-07-15 17/week @ 2024-07-22 24/week @ 2024-07-29 25/week @ 2024-08-05 16/week @ 2024-08-12 12/week @ 2024-08-19 29/week @ 2024-08-26 9/week @ 2024-09-02 10/week @ 2024-09-09 16/week @ 2024-09-16 40/week @ 2024-09-23 17/week @ 2024-09-30

84 downloads per month
Used in 8 crates (4 directly)

MIT/Apache

260KB
6K SLoC

IROX CSV Encoder/Decoder

Inspired by Python's csv module, a very basic csv reader & writer.

The primary use-case of this library is interacting with unstructured, variably structured, or dubiously structured data. As such, you probably want the far more robust and far better implemented csv crate.

Goals:

  • Provide a String-based mechanism to read and write CSV files in rfc4180 format.
  • Handle mixed formats resiliently, such as mismatched and newlines within quotes.

Non-Goals:

  • Any interpretation of the contents of the CSV structure itself - everything is an owned String
  • serde support - if you need it, go use the csv crate.

Examples:

  • Straight Iteration:
use irox_csv::error::CSVError;

fn iter_example() -> Result<(), CSVError> {
    let mut input = irox_csv::CSVReader::new(std::io::stdin());
    loop {
        // iterate over each line of the input
        let line : Option<Vec<String>> = input.read_line()?;
        match line {
            Some(fields) => {
                // Use the individual fields of the CSV line
                println!("{:?}", fields); // fields : Vec<String>
            }
            None => {
                // EOF
                break;
            }
        }
    }
    Ok(())
}
  • Map Iteration:
use irox_csv::error::CSVError;

fn map_example() -> Result<(), CSVError> {
    let mut maps = irox_csv::CSVMapReader::new(std::io::stdin());
    loop {
        // iterate over each line of the input
        let maybe_row : Option<Row> = maps.next_row()?;
        match maybe_row {
            Some(row) => {
                // Use the individual fields of the CSV line as a key-value map
                // The keys are the column headers found in the first row, the values are the matching row entry
                let map = row.into_map_lossy();
                println!("{:?}", map); // map : BTree<column:String, rowVal:String>
            }
            None => {
                // EOF
                break;
            }
        }
    }
    Ok(())
}
  • Writing a CSV File using Maps:
fn map_writer_example() -> Result<(), CSVError> {
    let mut buf: Vec<u8> = Vec::new();
    let mut writer = CSVWriterBuilder::new()
        .with_columns(&["first", "second", "third"])
        .build(&mut buf);

    let mut map = BTreeMap::new();
    map.insert("first".to_string(), "firstColFirstRowVal".to_string());
    map.insert("second".to_string(), "secondColFirstRowVal".to_string());
    map.insert("third".to_string(), "thirdColFirstRowVal".to_string());
    writer.write_fields(&map)?;
    
    map.clear();
    map.insert("first".to_string(), "firstColSecondRowVal".to_string());
    map.insert("second".to_string(), "secondColSecondRowVal".to_string());
    map.insert("third".to_string(), "thirdColSecondRowVal".to_string());
    writer.write_fields(&map)?;
    
    Ok(())
}

will result in a buffer:

first,second,third
firstColFirstRowVal,secondColFirstRowVal,thirdColFirstRowVal
firstColSecondRowVal,secondColSecondRowVal,thirdColSecondRowVal

Dependencies