Crates.io | tv_lib |
lib.rs | tv_lib |
version | 0.1.2 |
source | src |
created_at | 2022-05-13 11:37:52.191982 |
updated_at | 2022-05-13 11:56:54.357072 |
description | A library port of tv cli |
homepage | |
repository | https://github.com/alexhallam/tv_lib |
max_upload_size | |
id | 585800 |
size | 81,234 |
This is a Rust library that allows users to grab csv text as formatted by the tv cli command line utility and use it as they see fit
fn main() {
// define path
let in_file_path: &str = "examples/data/uspop.csv";
// one liner
let vec_vec_str = tv_lib::print::read_csv(in_file_path);
//print
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.
Remember, the goal is tv is about data visualization. In any practical calculation it is important to have a long decimal for lat/lon?
Include in Cargo.toml
tv-lib = "0.1.0"
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
Some times it is too hard to want to go into a directory to copy and paste a chunk of code 😉.
fn main() {
// define path
let in_file_path: &str = "examples/data/uspop.csv";
// one liner
let vec_vec_str = tv_lib::print::read_csv(in_file_path);
//print
println!("{:?}", vec_vec_str);
}
// format_from_csv_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)
}
// print_csv_from_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);
}
use csv::Reader;
use csv::Writer;
use std::fs::File;
fn main() {
let in_file_path: &str = "examples/data/uspop.csv";
// csv::ReaderBuilder
let mut r: Reader<File> = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b',')
.from_path(in_file_path)
.unwrap();
// Collect items in reader
let rdr = r
.records()
.into_iter()
.map(|x| x.expect("a csv record"))
.collect::<Vec<_>>();
// convert items to wtr
let rows: usize = rdr.len();
let mut wtr = Writer::from_writer(vec![]);
for row in 0..rows {
wtr.write_record(&rdr[row]).unwrap();
}
// convert wtr to string
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();
// use tv_lib: print_from_csv_str
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() {
// path
let in_file_path: &str = "examples/data/uspop.csv";
// csv::ReaderBuilder
let mut r: Reader<File> = csv::ReaderBuilder::new()
.has_headers(false)
.delimiter(b',')
.from_path(in_file_path)
.unwrap();
// Collect items in reader
let rdr = r
.records()
.into_iter()
.map(|x| x.expect("a csv record"))
.collect::<Vec<_>>();
// convert items to wtr
let rows: usize = rdr.len();
let mut wtr = Writer::from_writer(vec![]);
for row in 0..rows {
wtr.write_record(&rdr[row]).unwrap();
}
// convert wtr to string
let data = String::from_utf8(wtr.into_inner().unwrap()).unwrap();
// use tv_lib: print_from_csv_str
tv_lib::print::print_from_csv_str(data.as_str());
}