Skip to content

Commit 0dfc72e

Browse files
committed
improved CSV reader options
1 parent 76e3f6a commit 0dfc72e

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

cmd/csv2json/csv2json.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Convert data1.csv to JSON blobs, one line per blob
7171
delimiter string
7272
lazyQuotes bool
7373
trimLeadingSpace bool
74+
fieldsPerRecord int
75+
reuseRecord bool
7476
)
7577

7678
func main() {
@@ -100,6 +102,8 @@ func main() {
100102
app.StringVar(&delimiter, "d,delimiter", "", "set the delimter character")
101103
app.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quotes for for CSV input")
102104
app.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trim leading space in fields for CSV input")
105+
app.BoolVar(&reuseRecord, "reuse-record", false, "reuse the backing array")
106+
app.IntVar(&fieldsPerRecord, "fields-per-record", 0, "Set the number of fields expected in the CSV read, -1 to turn off")
103107

104108
// Parse environment and options
105109
app.Parse()
@@ -150,8 +154,11 @@ func main() {
150154
rowNo := 0
151155
fieldNames := []string{}
152156
r := csv.NewReader(app.In)
157+
r.Comment = '#'
158+
r.FieldsPerRecord = fieldsPerRecord
153159
r.LazyQuotes = lazyQuotes
154160
r.TrimLeadingSpace = trimLeadingSpace
161+
r.ReuseRecord = reuseRecord
155162
if delimiter != "" {
156163
r.Comma = datatools.NormalizeDelimiterRune(delimiter)
157164
}

cmd/tab2csv/tab2csv.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ This would yield
4949
showVersion bool
5050

5151
// CSV Reader Options
52-
lazyQuotes bool
53-
trimLeadingSpaces bool
54-
reuseRecord bool
55-
fieldsPerRecord int
52+
lazyQuotes bool
53+
trimLeadingSpace bool
54+
reuseRecord bool
55+
fieldsPerRecord int
5656
)
5757

5858
func main() {
@@ -65,8 +65,8 @@ func main() {
6565

6666
// CSV Reader options
6767
flag.IntVar(&fieldsPerRecord, "fields-per-record", 0, "sets the number o fields expected in each row, -1 turns this off")
68-
flag.BoolVar(&lazyQuotes, "lazy-quotes", false, "use lazy quoting for reader")
69-
flag.BoolVar(&trimLeadingSpaces, "left-trim", false, "trims leading space read")
68+
flag.BoolVar(&lazyQuotes, "use-lazy-quotes", false, "use lazy quoting for reader")
69+
flag.BoolVar(&trimLeadingSpace, "trim-leading-space", false, "trims leading space read")
7070
flag.BoolVar(&reuseRecord, "reuse-record", false, "re-uses the backing array on reader")
7171

7272
// Parse Environment and Options
@@ -92,7 +92,7 @@ func main() {
9292
r.Comment = '#'
9393
r.FieldsPerRecord = fieldsPerRecord
9494
r.LazyQuotes = lazyQuotes
95-
r.TrimLeadingSpace = trimLeadingSpaces
95+
r.TrimLeadingSpace = trimLeadingSpace
9696
r.ReuseRecord = reuseRecord
9797

9898
exitCode := 0

0 commit comments

Comments
 (0)