rust-csv

Rust的CSV解析器,支持Serde。(A CSV parser for Rust, with Serde support.)

Github星跟踪图

csv

A fast and flexible CSV reader and writer for Rust, with support for Serde.

Linux build status
Windows build status

Dual-licensed under MIT or the UNLICENSE.

Documentation

https://docs.rs/csv

If you're new to Rust, the
tutorial
is a good place to start.

Usage

Add this to your Cargo.toml:

[dependencies]
csv = "1.1"

Example

This example shows how to read CSV data from stdin and print each record to
stdout.

There are more examples in the
cookbook.

use std::error::Error;
use std::io;
use std::process;

fn example() -> Result<(), Box<dyn Error>> {
    // Build the CSV reader and iterate over each record.
    let mut rdr = csv::Reader::from_reader(io::stdin());
    for result in rdr.records() {
        // The iterator yields Result<StringRecord, Error>, so we check the
        // error here.
        let record = result?;
        println!("{:?}", record);
    }
    Ok(())
}

fn main() {
    if let Err(err) = example() {
        println!("error running example: {}", err);
        process::exit(1);
    }
}

The above example can be run like so:

$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-basic < examples/data/smallpop.csv

Example with Serde

This example shows how to read CSV data from stdin into your own custom struct.
By default, the member names of the struct are matched with the values in the
header record of your CSV data.

use std::error::Error;
use std::io;
use std::process;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Record {
    city: String,
    region: String,
    country: String,
    population: Option<u64>,
}

fn example() -> Result<(), Box<dyn Error>> {
    let mut rdr = csv::Reader::from_reader(io::stdin());
    for result in rdr.deserialize() {
        // Notice that we need to provide a type hint for automatic
        // deserialization.
        let record: Record = result?;
        println!("{:?}", record);
    }
    Ok(())
}

fn main() {
    if let Err(err) = example() {
        println!("error running example: {}", err);
        process::exit(1);
    }
}

The above example can be run like so:

$ git clone git://github.com/BurntSushi/rust-csv
$ cd rust-csv
$ cargo run --example cookbook-read-serde < examples/data/smallpop.csv

主要指标

概览
名称与所有者BurntSushi/rust-csv
主编程语言Rust
编程语言Rust (语言数: 3)
平台Cross-platform
许可证The Unlicense
所有者活动
创建于2014-03-22 06:58:51
推送于2025-04-05 11:27:48
最后一次提交2025-04-05 04:27:48
发布数98
最新版本名称csv-core-0.1.12 (发布于 2025-02-12 11:08:32)
第一版名称0.2.0 (发布于 )
用户参与
星数1.8k
关注者数18
派生数230
提交数466
已启用问题?
问题数239
打开的问题数59
拉请求数67
打开的拉请求数30
关闭的拉请求数37
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?