Purr-fectly parse your CSV files with delightful simplicity!
NyaCSV is a playful yet powerful CSV parsing and manipulation library for MoonBit that handles your data with cat-like grace and precision. Say goodbye to data hairballs and hello to elegantly structured information!
- Multi-source Input - Parse CSV from strings, binary data, or buffers... any data format a curious cat might find!
- Custom Delimiters - Not just commas! Use any character to separate your fields (because cats don't follow rules)
- Smart Quote Handling - Expertly handles quoted fields with special characters, like a cat delicately carrying its favorite toy
- Header Management - Auto-detect or generate column headers with feline intelligence
- Whitespace Grooming - Option to trim leading and trailing spaces, keeping your data as neat as a well-groomed kitty
- Pretty Printed Output - Display your data in beautiful table format that would make any cat proud to show off
- Flexible Configuration - Customize parsing behavior to match your exact needs, as demanding as the pickiest cat
fn main() {
// Parse a simple CSV string
let data = "Name,Age,Favorite Toy\nMittens,3,Ball of yarn\nWhiskers,5,Laser pointer"
let csv = CSV::parse_string(data)
// Display as a pretty table
println!("{}", csv)
// Access data directly
println!("The first cat's name is: {}", csv.rows[0][0])
}
Add NyaCSV to your MoonBit project:
moon add xunyoyo/NyaCSV
let options : CSVOptions = {
delimiter: ';',
allow_newlines_in_quotes: true,
quote_char: '"',
skip_empty_lines: true,
trim_spaces: true
}
let cat_data = "Name;Description;Adopted\nSimba;\"Playful orange tabby\nLoves treats\";Yes"
let csv = CSV::parse_string(cat_data, options~)
// Start with an empty CSV
let csv = CSV::new()
// Add headers and data programmatically
// ...
// Convert back to string
let csv_string = csv.to_string()
let cat_shelter_data = [
["Name", "Age", "Color", "Adopted"],
["Luna", "2", "Black", "Yes"],
["Oliver", "1", "Tabby", "No"],
["Bella", "4", "Calico", "Yes"]
]
let csv = CSV::from_array(cat_shelter_data)
println!("{}", csv)
struct CSV {
headers : Array[String]
rows : Array[Array[String]]
}
struct CSVOptions {
delimiter : Char // Default: ','
allow_newlines_in_quotes : Bool // Default: true
quote_char : Char // Default: '"'
skip_empty_lines : Bool // Default: true
trim_spaces : Bool // Default: false
}
CSV::parse_string(data: String, options: CSVOptions) -> CSV
- Parse CSV from stringCSV::parse_buffer(data: Buffer, options: CSVOptions) -> CSV
- Parse CSV from bufferCSV::parse_bytes(data: Bytes, options: CSVOptions) -> CSV
- Parse CSV from bytes
CSV::new() -> CSV
- Create empty CSVCSV::from_array(data: Array[Array[String]], has_header: Bool = true, generate_headers: Bool = true) -> CSV
- Create CSV from 2D array
to_string() -> String
- Convert CSV back to string formatoutput(logger) -> Unit
- Display as a pretty formatted table
let cat_breeds = "Breed,Origin,Size,Temperament\nSiamese,Thailand,Medium,\"Vocal, Active\"\nMaine Coon,USA,Large,\"Gentle, Playful\"\nRagdoll,USA,Large,\"Calm, Affectionate\""
let csv = CSV::parse_string(cat_breeds)
// Print as a gorgeous table
println!("{}", csv)
// Output:
// +------------+---------+--------+--------------------+
// | Breed | Origin | Size | Temperament |
// +------------+---------+--------+--------------------+
// | Siamese | Thailand| Medium | Vocal, Active |
// | Maine Coon | USA | Large | Gentle, Playful |
// | Ragdoll | USA | Large | Calm, Affectionate |
// +------------+---------+--------+--------------------+
let cat_profiles = "Name,Bio\nLucy,\"Lucy is a curious cat.\nShe loves exploring.\"\nMax,\"Max is lazy.\nHe sleeps all day.\""
let csv = CSV::parse_string(cat_profiles)
println!("{}", csv)
Contributions are welcome! Feel free to submit issues and pull requests to help make NyaCSV even more paw-some.
Apache-2.0
Made with <3 by cat lovers, for data lovers