tv lib example
fn main() {
let in_file_path: &str = "examples/data/uspop.csv";
let vec_vec_str = tv_lib::print::read_csv(in_file_path);
println!("{:?}", vec_vec_str[2][0..6].to_vec());
println!("{:?}", vec_vec_str[3][0..6].to_vec());
}
["Population", "NA", "7610", "NA", "NA", "NA"]
["Latitude", "65.2", "60.5", "33.7", "31.7", "32.3"]
In this small example see the features ported from tv.
- Missing values are filled with NA
- Latitude is normally a decimal that goes out 7 places! See that the sig figs are set to only show one significant digit.
Remember, the goal is tv is about data visualization. In any practical calculation it is important to have a long decimal for lat/lon?
Installation
Include in Cargo.toml
tv-lib = "0.1.0"
Examples
Examples may be run with cargo run --example <file in examples dir>
The following are available
cargo run --example short_read_csv
cargo run --example print_csv_from_str
cargo run --example format_csv_from_str
cargo run --example read_then_format_csv_from_str
cargo run --example read_then_print_csv_from_str
Examples Explicit
Some times it is too hard to want to go into a directory to copy and paste a chunk of code 😉.
Format from strings
fn main() {
let in_file_path: &str = "examples/data/uspop.csv";
let vec_vec_str = tv_lib::print::read_csv(in_file_path);
println!("{:?}", vec_vec_str);
}
fn main() {
let data = "\
City,State,Population,Latitude,Longitude
Davidsons Landing,AK,,65.2419444,-165.2716667
Kenai,AK,7610,60.5544444,-151.2583333
Oakman,AL,,33.7133333,-87.3886111";
let vec_vec_str = tv_lib::print::format_from_csv_str(data);
println!("{:?}",vec_vec_str)
}
fn main() {
let data = "\
City,State,Population,Latitude,Longitude
Davidsons Landing,AK,,65.2419444,-165.2716667
Kenai,AK,7610,60.5544444,-151.2583333
Oakman,AL,,33.7133333,-87.3886111";
tv_lib::print::print_from_csv_str(data);
}
Read CSV Then Format
use csv::Reader;
use csv::Writer;
use std::fs::File;
fn main() {
let in_file_path: &str = "examples/data/uspop.csv";
let mut r: Reader<File> = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b',')
.from_path(in_file_path)
.unwrap();
let rdr = r
.records()
.into_iter()
.map(|x| x.expect("a csv record"))
.collect::<Vec<_>>();
let rows: usize = rdr.len();
let mut wtr = Writer::from_writer(vec![]);
for row in 0..rows {
wtr.write_record(&rdr[row]).unwrap();
}
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();
let vec_vec_str = tv_lib::print::format_from_csv_str(data.as_str());
println!("{:?}", vec_vec_str[1][0..6].to_vec());
println!("{:?}", vec_vec_str[1][0..6].to_vec());
println!("{:?}", vec_vec_str[2][0..6].to_vec());
println!("{:?}", vec_vec_str[3][0..6].to_vec());
println!("{:?}", vec_vec_str[4][0..6].to_vec());
}
use csv::Reader;
use csv::Writer;
use std::fs::File;
fn main() {
let in_file_path: &str = "examples/data/uspop.csv";
let mut r: Reader<File> = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b',')
.from_path(in_file_path)
.unwrap();
let rdr = r
.records()
.into_iter()
.map(|x| x.expect("a csv record"))
.collect::<Vec<_>>();
let rows: usize = rdr.len();
let mut wtr = Writer::from_writer(vec![]);
for row in 0..rows {
wtr.write_record(&rdr[row]).unwrap();
}
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();
tv_lib::print::print_from_csv_str(data.as_str());
}