Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible column counts #176

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions data/unequal_lengths.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ID,Lat,Lon,YYYYMMDDHHmm,TL,TLSTA,RRL1c,RRS1c,RR6,WWL6,WWS3,RRS3c,R650,RC,TS,TD
202207031100
A006,54.88920,8.90870,202207031200,22.8,0.8,0.0,0.0,0.0,6.0,0.0,0.0,0.0,1,45.27,18.8
A006,54.88920,8.90870,202207031300,23.3,2.1,0.0,0.0,0.0,4.0,0.0,0.0,0.0,1,44.33,18.0
A006,54.88920,8.90870,202207031400,23.1,3.0,0.0,0.0,0.0,8.0,0.0,0.0,0.0,1,42.62,17.5
33 changes: 26 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ struct Cli {
help = "Print all rows in file. May be piped to 'less -S'. Example `tidy-viewer data/diamonds.csv -f -a | less -R`"
)]
force_all_rows: bool,
#[structopt(
short = "j",
long = "jump-invalid-rows",
help = "Jump over (skip) invalid rows in the file. This includes rows with the incorrect number of columns."
)]
skip_invalid_rows: bool,
#[structopt(
short = "p",
long = "pedantic",
help = "Crashes when csv input is malformed. Useful to check for valid csv data."
)]
pedantic: bool,
#[structopt(
short = "t",
long = "title",
Expand Down Expand Up @@ -645,12 +657,18 @@ fn main() {
return;
};

let rdr = r
.records()
.into_iter()
//.take(row_display_option + 1)
.map(|x| x.expect("a csv record"))
.collect::<Vec<_>>();
let rdr = r.records().collect::<Vec<_>>();
//.take(row_display_option + 1);

let rdr = if opt.skip_invalid_rows {
rdr.into_iter()
.filter_map(|record| record.ok())
.collect::<Vec<_>>()
} else {
rdr.into_iter()
.map(|record| record.expect("valid csv data"))
.collect::<Vec<_>>()
};

if debug_mode {
println!("{:?}", "StringRecord");
Expand Down Expand Up @@ -693,7 +711,7 @@ fn main() {
let column = rdr
.iter()
.take(rows)
.map(|row| row.get(col).unwrap())
.map(|row| row.get(col).unwrap_or_default())
.collect();
v.push(column)
}
Expand Down Expand Up @@ -1181,6 +1199,7 @@ fn build_reader(opt: &Cli) -> Result<Reader<Box<dyn Read>>, std::io::Error> {
}

let reader = ReaderBuilder::new()
.flexible(!(opt.pedantic || opt.skip_invalid_rows))
.has_headers(false)
.delimiter(delimiter)
.from_reader(source);
Expand Down