Skip to content

Latest commit

 

History

History
37 lines (28 loc) · 842 Bytes

delimiter.zh.md

File metadata and controls

37 lines (28 loc) · 842 Bytes

读取具有不同分隔符的 csv 记录

[![csv-badge]][csv] [![cat-encoding-badge]][cat-encoding]

用一个 tab(分隔符) delimiter 读取 csv 记录。

extern crate csv;
use csv::Error;
#[macro_use]
extern crate serde_derive;

#[derive(Debug, Deserialize)]
struct Record {
    name: String,
    place: String,
    #[serde(deserialize_with = "csv::invalid_option")]
    id: Option<u64>,
}

use csv::ReaderBuilder;

fn main() -> Result<(), Error> {
    let data = "name\tplace\tid
		Mark\tMelbourne\t46
		Ashley\tZurich\t92";

    let mut reader = ReaderBuilder::new().delimiter(b'\t').from_reader(data.as_bytes());
    for result in reader.deserialize::<Record>() {
        println!("{:?}", result?);
    }

    Ok(())
}