Splitting CSV into header and body.
This commit is contained in:
parent
5f1ad3e756
commit
71e016a3aa
1 changed files with 20 additions and 0 deletions
|
@ -29,6 +29,7 @@
|
||||||
csv-simple
|
csv-simple
|
||||||
(
|
(
|
||||||
csv-parse
|
csv-parse
|
||||||
|
csv-split-header
|
||||||
csv-simple-tests!
|
csv-simple-tests!
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -86,6 +87,19 @@
|
||||||
(csv-parse-line (make-csv-line-parser separator string-delimiter)))
|
(csv-parse-line (make-csv-line-parser separator string-delimiter)))
|
||||||
(map csv-parse-line lines)))
|
(map csv-parse-line lines)))
|
||||||
|
|
||||||
|
;; Splits CSV into header and body based on the first empty row.
|
||||||
|
(define (csv-split-header csv)
|
||||||
|
(let loop ((body csv)
|
||||||
|
(rhead '()))
|
||||||
|
(if (null? body)
|
||||||
|
(list (reverse rhead) '())
|
||||||
|
(let ((row (car body)))
|
||||||
|
(if (null? row)
|
||||||
|
(list (reverse rhead)
|
||||||
|
(cdr body))
|
||||||
|
(loop (cdr body)
|
||||||
|
(cons row rhead)))))))
|
||||||
|
|
||||||
;; Module self-tests
|
;; Module self-tests
|
||||||
(define (csv-simple-tests!)
|
(define (csv-simple-tests!)
|
||||||
(run-tests
|
(run-tests
|
||||||
|
@ -96,6 +110,12 @@
|
||||||
(test-equal? csv-parse-line
|
(test-equal? csv-parse-line
|
||||||
((make-csv-line-parser ";" "\"") "test;\"2;quoted\";3")
|
((make-csv-line-parser ";" "\"") "test;\"2;quoted\";3")
|
||||||
'("test" "2;quoted" "3"))
|
'("test" "2;quoted" "3"))
|
||||||
|
(test-equal? csv-split-header
|
||||||
|
(csv-split-header '((1 2) () (3 4)))
|
||||||
|
'(((1 2)) ((3 4))))
|
||||||
|
(test-equal? csv-split-header
|
||||||
|
(csv-split-header '((1 2) (5 6) (3 4)))
|
||||||
|
'(((1 2) (5 6) (3 4)) ()))
|
||||||
))
|
))
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue