Skip to content

Commit 00ff646

Browse files
authored
Flexible column counts (#176)
* Initial implementation for flexible column counts * add pedantic flag, better cleaner code * fix comments * remove dbg
1 parent 204a232 commit 00ff646

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

data/unequal_lengths.csv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ID,Lat,Lon,YYYYMMDDHHmm,TL,TLSTA,RRL1c,RRS1c,RR6,WWL6,WWS3,RRS3c,R650,RC,TS,TD
2+
202207031100
3+
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
4+
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
5+
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

src/main.rs

+26-7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ struct Cli {
7878
help = "Print all rows in file. May be piped to 'less -S'. Example `tidy-viewer data/diamonds.csv -f -a | less -R`"
7979
)]
8080
force_all_rows: bool,
81+
#[structopt(
82+
short = "j",
83+
long = "jump-invalid-rows",
84+
help = "Jump over (skip) invalid rows in the file. This includes rows with the incorrect number of columns."
85+
)]
86+
skip_invalid_rows: bool,
87+
#[structopt(
88+
short = "p",
89+
long = "pedantic",
90+
help = "Crashes when csv input is malformed. Useful to check for valid csv data."
91+
)]
92+
pedantic: bool,
8193
#[structopt(
8294
short = "t",
8395
long = "title",
@@ -645,12 +657,18 @@ fn main() {
645657
return;
646658
};
647659

648-
let rdr = r
649-
.records()
650-
.into_iter()
651-
//.take(row_display_option + 1)
652-
.map(|x| x.expect("a csv record"))
653-
.collect::<Vec<_>>();
660+
let rdr = r.records().collect::<Vec<_>>();
661+
//.take(row_display_option + 1);
662+
663+
let rdr = if opt.skip_invalid_rows {
664+
rdr.into_iter()
665+
.filter_map(|record| record.ok())
666+
.collect::<Vec<_>>()
667+
} else {
668+
rdr.into_iter()
669+
.map(|record| record.expect("valid csv data"))
670+
.collect::<Vec<_>>()
671+
};
654672

655673
if debug_mode {
656674
println!("{:?}", "StringRecord");
@@ -693,7 +711,7 @@ fn main() {
693711
let column = rdr
694712
.iter()
695713
.take(rows)
696-
.map(|row| row.get(col).unwrap())
714+
.map(|row| row.get(col).unwrap_or_default())
697715
.collect();
698716
v.push(column)
699717
}
@@ -1181,6 +1199,7 @@ fn build_reader(opt: &Cli) -> Result<Reader<Box<dyn Read>>, std::io::Error> {
11811199
}
11821200

11831201
let reader = ReaderBuilder::new()
1202+
.flexible(!(opt.pedantic || opt.skip_invalid_rows))
11841203
.has_headers(false)
11851204
.delimiter(delimiter)
11861205
.from_reader(source);

0 commit comments

Comments
 (0)