~ubuntu-branches/ubuntu/vivid/cl-csv/vivid-proposed

« back to all changes in this revision

Viewing changes to data-table.lisp

  • Committer: Package Import Robot
  • Author(s): Dimitri Fontaine
  • Date: 2014-08-04 19:57:54 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20140804195754-vo64b5r1daxwg8ld
Tags: 20140211-1
Quicklisp release update.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(cl:in-package :cl-csv)
 
2
 
 
3
(defun get-data-table-from-csv (file &optional (has-column-names t) (munge-types t) sample
 
4
                                &aux (dt (make-instance 'data-table:data-table)))
 
5
  "Gets a data-table object representing the CSV"
 
6
  (iter
 
7
    (for row in-csv file)
 
8
    (for data = (mapcar #'data-table::trim-and-nullify row))
 
9
    (if (and has-column-names (first-iteration-p))
 
10
        (setf (data-table:column-names dt) data)
 
11
        (if sample
 
12
            (sampling data into samples size sample)
 
13
            (collect data into rows)))
 
14
    (finally
 
15
     (setf (data-table:rows dt)
 
16
           (if sample samples rows))))
 
17
  (if munge-types
 
18
      (data-table:coerce-data-table-of-strings-to-types dt)
 
19
      (data-table::ensure-column-data-types dt))
 
20
  dt)
 
21
 
 
22
 
 
23
(defun data-table-to-csv (dt &optional stream)
 
24
  "Write a datatable object out to csv"
 
25
  (write-csv (list* (data-table:column-names dt) (data-table:rows dt))
 
26
             :stream stream))
 
27
 
 
28
(defun get-data-table-from-csv-list
 
29
    (list &optional (has-column-names t) (munge-types t) sample
 
30
          &aux (dt (make-instance 'data-table:data-table)))
 
31
  "Create a data-table from the parsed csv as lisp lists"
 
32
  (flet ((map-fn (row) (mapcar #'data-table::trim-and-nullify row)))
 
33
    (when has-column-names
 
34
      (setf (data-table:column-names dt) (map-fn (first list))))
 
35
    (setf (data-table:rows dt)
 
36
          (mapcar (lambda (x)
 
37
                    (if sample
 
38
                        (map-fn (subseq x 0 sample))
 
39
                        (map-fn x)))
 
40
                  (rest list)))
 
41
    (when munge-types
 
42
      (data-table:coerce-data-table-of-strings-to-types dt))
 
43
    dt))
 
44