Skip to content

A simple CSV Reader/Writer library created for personal projects.

License

Notifications You must be signed in to change notification settings

WillDeJs/csvlib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSVLIB

A simple Rust CSV Reader/Writer library with a simple API. Implements Reader, Writer and Document structures.

Reader

Read and iterate throw CSV files easily. See CSV Reader example below.

Example (Reader):

// Open a CSV file to read.
let csv_reader = csvlib::Reader::from_path("./AAPL.csv").unwrap();

// Iterate through rows
println!("{}", csv_reader.headers().unwrap());
for entry in csv_reader.entries() {
   println!("{}", entry);
}

Writer

Create CSV files and write rows easily. See CSV Writer example.

Example (Writer):

// Create a writer from a file path
let mut writer = csvlib::Writer::from_path("./test.csv").unwrap();

// Write rows to file
writer.write_all(&[
       csvlib::csv!["Header1", "Header2", "Header3"]
       csvlib::csv!["entry11", "entry12", "entry13"],
       csvlib::csv!["entry21", "entry22", "entry23"],
       csvlib::csv!["entry31", "entry32", "entry33"],
       csvlib::csv!["entry41", "entry42", "entry43"],
   ])
   .unwrap();

Document

Easily open a document and search through it.

 use csvlib::Document;
 let doc = Document::from_path("students.csv").expect("Could not open file");

 // Get some field values
 let ages = doc.get_column::<i32>("Age").unwrap();
 let emails = doc.get_column::<String>(&String::from("Email")).unwrap();
 let schools = doc.get_column::<String>("School").unwrap();

Additionally modify, filter and save values inside of the document and make copies if needed.

use csvlib::{CsvError, Document};

fn main() -> Result<(), CsvError> {
    // Open document
    let mut students_document = Document::from_path(r#"students.csv"#).unwrap();

    // Filter the document based on the desired criteria
    students_document.retain(|entry| {
        // Keep students only from Springfield high
        match entry.get::<String>("school") {
            Ok(school_name) => school_name == "Springfield High School",
            _ => false,
        }
    });

    // Iterate through the rows in the document
    for mut student in students_document.rows_mut() {
        let name = student.get::<String>("name")?;
        let last_name = student.get::<String>("lastname")?;

        // Modify Rows only if needed
        if name == "Bartholomew" && last_name == "Simpson" {
            student.set("homework", 85);
            student.set("attendance", 100);
            student.set("final exam", 93);
        }
    }

    // Save filtered results
    students_document.write_to_file("Springfield Students.csv")
}

About

A simple CSV Reader/Writer library created for personal projects.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages