Rust : CSV库的用法

本文感谢42的帮助。

Rust有一个库CSV,可以用来处理相关的CSV文件。

相关资料见:http://burntsushi.net/rustdoc/csv/

比如有个CSV文件,其结构是(有表头的):

这里写图片描述

以下读出CSV,可以有两种组织方式:

解析成struct,非常方便,赞!

extern crate csv;
use std::time::{Duration, SystemTime};
use csv::{ReaderBuilder, StringRecord};
#[derive(Serialize, Deserialize, Debug)]

struct Bar{
    a1: String,
    a2: f64,
    a3: f64,
    a4: f64,
    a5: f64,
    a6: f64,
    a7: f64,
    a8: f64,
    a9: f64,
    a10: f64,
    a11: f64,
    a12: f64,
}

fn csv_to_struct(path:String,has_header:bool)-> Result<VBar,std::io::Error>{
    let mut vb :VBar = VBar{
        a1: Vec::new(),
        a2: Vec::new(),
        a3: Vec::new(),
        a4: Vec::new(),
        a5: Vec::new(),
        a6: Vec::new(),
        a7: Vec::new(),
        a8: Vec::new(),
        a9: Vec::new(),
        a10: Vec::new(),
        a11: Vec::new(),
        a12: Vec::new(),
    };
    let mut rdr =
         ReaderBuilder::new().has_headers(false).from_path(path).unwrap();
    let records = rdr
             .records()
             .collect::<Result<Vec<StringRecord>, csv::Error>>()?;
    let mut i = 0;
    for record in records{
        let record = fill_nan(&record,0.0);
        i = i + 1;
        if has_header == true && i==1 {
            continue;
        }else{
            vb.a1.push(record[0].to_string());
            vb.a2.push(record[1].parse::<f64>().unwrap());
            vb.a3.push(record[2].parse::<f64>().unwrap());
            vb.a4.push(record[3].parse::<f64>().unwrap());
            vb.a5.push(record[4].parse::<f64>().unwrap());
            vb.a6.push(record[5].parse::<f64>().unwrap());
            vb.a7.push(record[6].parse::<f64>().unwrap());
            vb.a8.push(record[7].parse::<f64>().unwrap());
            vb.a9.push(record[8].parse::<f64>().unwrap());
            vb.a10.push(record[9].parse::<f64>().unwrap());
            vb.a11.push(record[10].parse::<f64>().unwrap());
            vb.a12.push(record[11].parse::<f64>().unwrap());
        }

    }
    Ok(vb)
}

三、关于struct的优化

如何把bar的结构精准地描述(f64,String…)?
我们发现:
如果去掉表头,形成以下模式,则可以把bar结构中字段进行精确解析。

这里写图片描述

extern crate stopwatch;
extern crate csv;
extern crate rustc_serialize;
use stopwatch::Stopwatch;
use std::thread;
use std::io;
use std::time::{Duration, Instant};

#[derive(RustcDecodable, RustcEncodable)]
struct bar {
    market: String,
    code: String,
    date: String,
    open: f64,
    high: f64,
    low: f64,
    close: f64,
    volume: f64,
    openInterests: f64,
}
fn main() {
    let mut rdr =
      csv::Reader::from_path("C:\\IC1505.csv").unwrap();//不要求是读出第一行段首,否则加.has_headers().
    let mut data: Vec<bar> = Vec::new();
    for record in rdr.decode() {
        let mut temp: bar = record.unwrap();
        println!("len: =>{:?},{},{},{},{}",
                 data.len(),
                 temp.market,
                 temp.code,
                 temp.date,
                 temp.close);
        data.push(temp);
    }
    thread::sleep_ms(500000);
}

四、csv库如何读出汉字?
比如:
这里写图片描述


extern crate csv;
extern crate rustc_serialize;
extern crate encoding;

use std::io;
use std::io::prelude::*;
use std::fs::File;

use encoding::{Encoding,  DecoderTrap};
use encoding::all::GB18030;//可以转成汉字的字库

fn main() {
    let path = "C:\\Users\\Desktop\\test.csv";
    let mut f = File::open(path).ok().expect("cannot open file"); 
    let mut reader: Vec<u8> = Vec::new();
    f.read_to_end(&mut reader).ok().expect("can not read file");
    let mut chars = String::new();
    GB18030.decode_to(&mut reader, DecoderTrap::Ignore, &mut chars);
    let mut rdr = csv::Reader::from_string(chars).has_headers(true);
    for row in rdr.decode() {
        let (x, y, r): (String, String, String) = row.unwrap();
        println!("({}, {}): {:?}", x, y, r);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值