Giter VIP home page Giter VIP logo

rust-csv's Introduction

This crate provides a streaming CSV (comma separated values) writer and reader that works with the serialize crate to do type based encoding and decoding. There are two primary goals of this project:

  1. The default mode of parsing should just work. This means the parser will bias toward providing a parse over a correct parse (with respect to RFC 4180).
  2. Convenient to use by default, but when performance is needed, the API will provide an escape hatch.

Build status

Licensed under the UNLICENSE.

Documentation

The API is fully documented with lots of examples: http://burntsushi.net/rustdoc/csv/.

Simple examples

Here is a full working Rust program that decodes records from a CSV file. Each record consists of two strings and an integer (the edit distance between the strings):

extern crate csv;

use std::path::Path;

fn main() {
    let fp = &Path::new("./data/simple.csv");
    let mut rdr = csv::Reader::from_file(fp);

    for record in rdr.decode() {
        let (s1, s2, dist): (String, String, uint) = record.unwrap();
        println!("({}, {}): {}", s1, s2, dist);
    }
}

Don't like tuples? That's fine. Use a struct instead:

extern crate csv;
extern crate serialize;

use std::path::Path;

#[deriving(Decodable)]
struct Record {
    s1: String,
    s2: String,
    dist: uint,
}

fn main() {
    let fp = &Path::new("./data/simple.csv");
    let mut rdr = csv::Reader::from_file(fp);

    for record in rdr.decode() {
        let record: Record = record.unwrap();
        println!("({}, {}): {}", record.s1, record.s2, record.dist);
    }
}

Do some records not have a distance for some reason? Use an Option type!

#[deriving(Decodable)]
struct Record {
    s1: String,
    s2: String,
    dist: Option<uint>,
}

You can also read CSV headers, change the delimiter, use enum types or just get plain access to records as vectors of strings. There are examples with more details in the documentation.

Installation

This crate works with Cargo. Assuming you have Rust and Cargo installed, simply check out the source and run tests:

git checkout git://github.com/BurntSushi/rust-csv
cd rust-csv
cargo test

You can also add rust-csv as a dependency to your project's Cargo.toml:

[dependencies.rust-csv]
git = "git://github.com/BurntSushi/rust-csv"

Benchmarks

There are some rough benchmarks (compared with Go) here: https://github.com/BurntSushi/rust-csv/tree/master/bench

Related work

The only other one I know is arjantop/rust-tabular, which does not support the Encoder or Decoder API. However, it does support parsing fixed-width tables.

rust-csv's People

Contributors

burntsushi avatar brandonw avatar ebfe avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.